2012-02-23 63 views
1

我试图使用LinkedIn API访问网络外的配置文件,遵循here的说明。
我正在用Python编写我的代码。LinkedIn离开网络API调用返回“未授权”错误

据我了解,我只需要添加一个额外的头我HTTPSConnection要求:

正常的呼叫:

connection.request(method, relative_url, body = body, headers={'Authorization': OAuth_header})

外的网络呼叫:

// these values were extracted from the http-header I received
name = x-li-auth-token
value = name:R8Y4
connection.request(method, relative_url, body = body, headers={'Authorization': OAuth_header, name: value})

当我进行网络外呼叫时,出现错误:

error:
status: 401
timestamp: 1330027911625
request-id: VHUSL0J7TL
error-code: 0
message: [unauthorized]. OAU:k1tofeoqr4id|2dc38f4e-73d1-4d31-9330-dd82aca89616|*01|*01:1330027911:CRc7YYYQRe2VS6woJpGX+qYVa/Q=

我一直在我自己的配置文件和实际的网络外配置文件上进行测试,没有发生错误更改。
从各种API的请求,将“值”略改变,我已经尝试了所有的变体:

"name:R8Y4"
"search-name:R8Y4"
"OUT_OF_NETWORK:R8Y4"

我猜它与HTTP头的事,但我不知道什么是错的。

请帮忙!谢谢。

回答

3

我不确定你的通话失败的原因。以下是使用python中测试过的oauth2库的序列,包括整个HTTP对话。

首先,做搜索: http://api.linkedin.com/v1/people-search:(people:(distance,id,first-name,last-name,headline,api-standard-profile-request))

Parameters (I'm using OAuth in the parameters for this call) 
oauth_body_hash=2jmj7l5rSw0yVb%2FvlWAYkK%2FYBwk%3D 
oauth_nonce=41358038 
oauth_timestamp=1330098205 
oauth_consumer_key=xxx 
oauth_signature_method=HMAC-SHA1 
facet=network%2CO 
oauth_version=1.0 
oauth_token=xxx 
keywords=Schneider+Electric 
oauth_signature=xxx 

Response includes: 
<person> 
    <distance>-1</distance> 
    <id>UBAQYFeiHo</id> 
    <first-name></first-name> 
    <last-name>Private</last-name> 
    <headline>Assistant Engineer at Schneider Electric</headline> 
    <api-standard-profile-request> 
    <url>http://api.linkedin.com/v1/people/UBAQYFeiHo</url> 
    <headers total="1"> 
     <http-header> 
     <name>x-li-auth-token</name> 
     <value>OUT_OF_NETWORK:wHti</value> 
     </http-header> 
    </headers> 
    </api-standard-profile-request> 
</person> 

第二个电话,获取配置文件: http://api.linkedin.com/v1/people/UBAQYFeiHo:(id,first-name,last-name

Request headers: 
Host: api.linkedin.com 
x-li-auth-token: OUT_OF_NETWORK:wHti 
accept-encoding: gzip, deflate 
user-agent: Python-httplib2/$Rev$ 

Response: 
{'status': '200', 'content-length': '158', 'content-location': u'http://api.linkedin.com/v1/people/UBAQYFeiHo:(id,first-name,last-name)?oauth_body_hash=2jmj7l5rSw0yVb%2FvlWAYkK%2FYBwk%3D&oauth_nonce=27886786&oauth_timestamp=1330098212&oauth_consumer_key=xxx&oauth_signature_method=HMAC-SHA1&oauth_version=1.0&oauth_token=xxx&oauth_signature=xxx', 'transfer-encoding': 'chunked', 'vary': '*', 'server': 'Apache-Coyote/1.1', '-content-encoding': 'gzip', 'date': 'Fri, 24 Feb 2012 15:43:34 GMT', 'x-li-request-id': 'N368G241EA', 'x-li-format': 'xml', 'content-type': 'text/xml;charset=UTF-8'} 
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<person> 
    <id>UBAQYFeiHo</id> 
    <first-name></first-name> 
    <last-name>Private</last-name> 
</person> 

的Python代码,以与第二部分工作的oauth2库:

import oauth2 as oauth 
import time 

url = "http://api.linkedin.com/v1/people/UBAQYFeiHo:(id,first-name,last-name)" 
consumer = oauth.Consumer(
key="xxx", 
secret="xxx") 

token = oauth.Token(
key="xxx", 
secret="xxx") 

client = oauth.Client(consumer, token) 

resp, content = client.request(url, headers={'x-li-auth-token':'OUT_OF_NETWORK:wHti'}) 
print resp 
print content 
+0

谢谢你再次Kirsten :) – joulesm 2012-02-27 22:13:32

+0

每个人都使用Python Oauth2库! API调用很简单,易于使用/理解。 – joulesm 2012-02-27 22:14:11

相关问题