Python Linkedin API OAuth2 HTTP错误401:未经授权

planet260

我正在尝试代表用户在Linkedin上分享帖子。我引导用户完成身份验证过程,以生成oauth2令牌。我有令牌,但是现在我被卡住了如何使用它。我在互联网上找到的所有帮助都是关于Oauth而不是Oauth2。我正在尝试发送请求,但我收到HTTP错误401:未经授权。下面是我的代码...

import urllib2, cookielib
cookie_jar = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie_jar))
urllib2.install_opener(opener)

xml_request = ..... XML

headers={'Content-Type': 'application/xml'}
url = "http://api.linkedin.com/v1/people/~/shares/?oauth2_access_token=XXXX"
req = urllib2.Request(url, data=xml_request, headers = headers)
rsp = opener.open(req)
content = rsp.read()

我已经检查过并且该令牌是有效的,我正在使用相同的令牌获取网络更新...我已经搜索并搜索了,但是在Oauth2上仍然没有帮助。我在使用Oauth而不是Oauth2时见过的所有Linkedin客户端。请在如何发送此请求方面帮助我。如果有人知道使用oauth2的任何api或客户端,请让我知道..在此先感谢您的帮助

planet260

我写了下面的代码以使用OAuth 2.0在linkedin上共享内容

import requests
import json
def make_request(method, url, token ,data=None, params=None, headers=None, timeout=60):
    headers = {'x-li-format': 'json', 'Content-Type': 'application/json'}
    params = {} 
    kw = dict(data=data, params=params, headers=headers, timeout=timeout)
    params.update({'oauth2_access_token': token})
    return requests.request(method.upper(), url, **kw)   

def submit_share(comment, title, description, submitted_url, submitted_image_url, token):
    post = {
        'comment': comment,
        'content': {
            'title': title,
            'submitted-url': submitted_url,
            'submitted-image-url': submitted_image_url,
            'description': description
        },
        'visibility': {
            'code': 'anyone'
        }
    }
    url = 'https://api.linkedin.com/v1/people/~/shares'
    try:
        response = make_request('POST', url, token,data=json.dumps(post))
        response = response.json()
        return response
    except Exception:
        return False

我希望这段代码对任何人都有帮助。问候

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章