Python - oauth2 - linkedin API

标签 python oauth linkedin

我正在尝试通过 python 代码并按照 LinkedIn API documentation 关注一些在 LinkedIn 上注册的公司。我需要使用oauth2 - POST 方法来关注一家公司。

我的查询如下:

  1. 如何通过python代码指定一个特定的公司名称来关注一个公司?
  2. 有人可以为此建议 python 代码吗?

我的代码如下:

oauth_token    = oauth.Token(key=access_token_key, secret=access_token_secret)
oauth_consumer = oauth.Consumer(key=api_key, secret=api_secret)
signature_method_hmac_sha1 = oauth.SignatureMethod_HMAC_SHA1()
http_method    = "POST"
http_handler   = urllib.HTTPHandler(debuglevel=_debug)
https_handler  = urllib.HTTPSHandler(debuglevel=_debug)

def linkedinreq(url, method, parameters):
          req = oauth.Request.from_consumer_and_token(oauth_consumer,
                                            token=oauth_token,
                                            http_method=http_method,
                                            http_url=url, 
                                            parameters=parameters)

          req.sign_request(signature_method_hmac_sha1, oauth_consumer, oauth_token)
          req.to_postdata()

def fetchsamples():
          url = "https://api.linkedin.com/v1/people/~/following/companies"

          parameters = []
          response = linkedinreq(url, "POST", parameters)

fetchsamples()

最佳答案

python 模块 python-linkedinpython-linkedin-v2 已过时。因此,我建议您改为使用 requests_oauthlib 模块。

from requests_oauthlib import OAuth2Session
from requests_oauthlib.compliance_fixes import linkedin_compliance_fix

# In case the `redirect_url` does not implement https     
import os                                                   
os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1' 

# Credentials you get from registering a new application
client_id = '<the client id you get from linkedin>'
client_secret = '<the client secret you get from linkedin>'
redirect_url = '<authorized redirect URL from LinkedIn config>'

# OAuth endpoints given in the LinkedIn API documentation (check for updates)
authorization_base_url = 'https://www.linkedin.com/oauth/v2/authorization'
token_url = 'https://www.linkedin.com/oauth/v2/accessToken'

# Authorized Redirect URL (from LinkedIn config)
linkedin = OAuth2Session(client_id, redirect_uri=redirect_url)
linkedin = linkedin_compliance_fix(linkedin)

# Redirect user to LinkedIn for authorization
authorization_url, state = linkedin.authorization_url(authorization_base_url)
print('Please go here and authorize,', authorization_url)

# Get the authorization verifier code from the callback url
redirect_response = input('Paste the full redirect URL here:')

# Fetch the access token
linkedin.fetch_token(token_url, client_secret=client_secret,
                     authorization_response=redirect_response)

# Fetch a protected resource, i.e. user profile
r = linkedin.get('https://api.linkedin.com/v1/people/~')
print(r.content)

关于Python - oauth2 - linkedin API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25597021/

相关文章:

python - 将某些索引名称(字符串)复制到新的数据帧

python - Django Autocomplete Light 创建新选择

python - Flask-login 没有按预期工作

objective-c - Twitter OAuth 和 accessToken 在 Objective-C 中获取状态/user_timeline

oauth - 客户端可以共享 Oauth2 访问 token 吗?

python - 如何按照Python教科书的要求实现 'class Card'

python - 批量训练但在 Tensorflow 中测试单个数据项?

java - 社交身份验证 - 链接在 OAuth 2 中不起作用

ios - 在 iOS 中获取特定网站 URL 的缩略图和标题?

python - 如何使用 Beautifulsoup 解析网站