首页
学习
活动
专区
圈层
工具
发布

oauth2
EN

Stack Overflow用户
提问于 2014-08-31 22:57:10
回答 2查看 3.1K关注 0票数 3

我试图通过python代码跟踪一些在LinkedIn上注册的公司,根据LinkedIn API文档,我需要使用oauth2 - POST方法来跟踪公司。

我的问题如下:

  1. 如何通过python代码指定特定的公司名称以跟随公司?
  2. 有人能就此向python代码提供建议吗?

我的代码如下:

代码语言:javascript
复制
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()
EN

回答 2

Stack Overflow用户

发布于 2018-06-11 18:26:52

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

代码语言:javascript
复制
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)
票数 8
EN

Stack Overflow用户

发布于 2014-08-31 23:20:38

而不是重新发明车轮,使用python-linkedin包装器。

用于搜索公司的示例代码:

代码语言:javascript
复制
from linkedin import linkedin

CONSUMER_KEY = 'your key'
CONSUMER_SECRET = 'your secret'
USER_TOKEN = 'your token'
USER_SECRET = 'your user secret'
RETURN_URL = ''

# Instantiate the developer authentication class
authentication = linkedin.LinkedInDeveloperAuthentication(CONSUMER_KEY, CONSUMER_SECRET,
                                                          USER_TOKEN, USER_SECRET, 
                                                          RETURN_URL, linkedin.PERMISSIONS.enums.values())

# Pass it in to the app...
application = linkedin.LinkedInApplication(authentication)

print application.search_company(selectors=[{'companies': ['name', 'universal-name', 'website-url']}],
                                 params={'keywords': 'apple microsoft'})

要跟踪公司,请使用follow_company()方法,请参阅更多信息和示例这里

代码语言:javascript
复制
COMPANY_ID = 1035  # this you would get from the `search_company()`
application.follow_company(COMPANY_ID)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25597021

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档