【问题标题】:Python Linkedin, How to share content on user's wallPython Linkedin,如何在用户的墙上分享内容
【发布时间】:2013-11-25 21:15:49
【问题描述】:

我正在构建一个允许通过 Linkedin 登录的应用程序。我没有使用linkedin api .. 所以我要做的是让用户通过身份验证过程,最后生成一个身份验证令牌(OAuth2)...... 使用这个令牌我得到它的更新和连接细节......使用 urllib

url = "https://api.linkedin.com/v1/people/~/network/updates?type=SHAR&count=50&start=50&oauth2_access_token=XXXX"
lp = urllib2.urlopen(url)

现在我需要做的是使用此令牌在用户的墙上分享。当我登录用户时,我已经拥有 rw_nus 访问权限...... 在文档中提到使用链接“http://api.linkedin.com/v1/people/~/shares”,但我对如何使用令牌在此 url 上发送 JSON 共享内容有点困惑......我正在做如下

share_object = {
"comment":"comment_text",
"content": {
    "title":"Test",
    "submitted_url":"http://www.test.com/",
},
"visibility": {
    "code": "anyone"
}
}
api_url = "http://api.linkedin.com/v1/people/~/shares?oauth2_access_token=XXXX";

data = json.dumps(share_object)
req = urllib2.Request(api_url, data, {'Content-Type': 'application/json'})
f = urllib2.urlopen(req)
response = f.read()
f.close()

它在行中给出了这个错误:f = urllib2.urlopen(req)

urllib2.HTTPError: HTTP Error 401: Unauthorized

【问题讨论】:

  • 你为什么不用Linkedin API客户端?
  • 如果我有身份验证令牌,那么我将如何使用 Linkedin API 客户端进行上述共享过程??
  • 我搜索过的所有linkedin客户端都在使用Oauth,但我已经使用Oauth2生成了令牌。我没有找到任何使用Oauth2的客户端或api...

标签: python python-2.7 linkedin


【解决方案1】:

我写了下面的函数来使用 OAuth2 在 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

我希望它可以帮助某人。 问候

【讨论】:

    猜你喜欢
    • 2017-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-06
    • 2011-10-20
    相关资源
    最近更新 更多