LinkedIn退出网络API调用返回“未授权”错误

时间:2012-02-23 16:49:04

标签: python api linkedin

我正按照here的说明尝试使用LinkedIn API访问网络外配置文件。
我正在用Python编写代码。

据我了解,我只是在我的HTTPSConnection请求中添加了一个额外的标头:

正常通话:

  

connection.request(方法,relative_url,body = body,headers = {'授权':OAuth_header})

网络外电话:

  

//这些值是从我收到的http-header中提取的       name = x-li-auth-token
      value = name:R8Y4
      connection.request(method,relative_url,body = body,headers = {'Authorization':OAuth_header,name:value})

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

  

错误:
    状态:401
    时间戳:1330027911625
    request-id:VHUSL0J7TL
    错误代码:0
    消息:[未经授权]。非统组织:k1tofeoqr4id | 2dc38f4e-73d1-4d31-9330-dd82aca89616 | * 01 | * 01:1330027911:CRc7YYYReReVS6woJpGX + qYVa / Q =

我一直在我自己的个人资料和实际的网络外配置文件中对此进行测试,错误没有变化。
从各种API请求中,“值”稍有变化,我尝试了所有变体:

  

“名称:R8Y4”
  “搜索名称:R8Y4”
  “OUT_OF_NETWORK:R8Y4”

我猜它与HTTP标头有关,但我不知道出了什么问题。

请帮忙!谢谢。

1 个答案:

答案 0 :(得分: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>

使第二部分与oauth2库一起工作的python代码是:

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