我正在使用此代码段
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
我从一条建议中看到了有关如何使用linkedin发布数据的信息。
尝试此操作时,我收到了无效的令牌错误
In [9]: submit_share('some comment', 'this title', 'this description', 'http://68.183.98.238/blog/best-floor-for-high-traffic-areas', 'http://68.183.98.238/me
...: dia/ckeditor/2018/11/20/12316203_864269787022150_6304427639285281585_n-768x512_dzXgMi1.jpg', token)
Out[9]:
{'errorCode': 0,
'message': 'Invalid access token.',
'requestId': '70CH99XQKF',
'status': 401,
'timestamp': 1548754831494}
令牌是有效的,我只是假设代码已弃用。
感谢对linkedin的Twitter帐户的快速响应,他们建议我尝试this的建议,我认为这可能会起作用,但其中并未涵盖如何发布至该帐户。
我已经处理了建议的示例,但它引发了一个问题,这是代码
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 = 'xxxxx'
client_secret = 'xxxxx'
redirect_url = 'http://decomagna.com'
# 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_url)
# Fetch a protected resource, i.e. user profile
r = linkedin.get('https://api.linkedin.com/v1/people/~')
print(r.content)
这是它引发的错误
---------------------------------------------------------------------------
MissingCodeError Traceback (most recent call last)
<ipython-input-1-f75f0c6c99c5> in <module>
28 # Fetch the access token
29 linkedin.fetch_token(token_url, client_secret=client_secret,
---> 30 authorization_response=redirect_url)
31
32 # Fetch a protected resource, i.e. user profile
~/code/envs/decomagna/lib/python3.6/site-packages/requests_oauthlib/oauth2_session.py in fetch_token(self, token_url, code, authorization_response, body, auth, username, password, method, timeout, headers, verify, proxies, include_client_id, client_secret, **kwargs)
206 if not code and authorization_response:
207 self._client.parse_request_uri_response(authorization_response,
--> 208 state=self._state)
209 code = self._client.code
210 elif not code and isinstance(self._client, WebApplicationClient):
~/code/envs/decomagna/lib/python3.6/site-packages/oauthlib/oauth2/rfc6749/clients/web_application.py in parse_request_uri_response(self, uri, state)
201 oauthlib.oauth2.rfc6749.errors.MismatchingStateError
202 """
--> 203 response = parse_authorization_code_response(uri, state=state)
204 self.populate_code_attributes(response)
205 return response
~/code/envs/decomagna/lib/python3.6/site-packages/oauthlib/oauth2/rfc6749/parameters.py in parse_authorization_code_response(uri, state)
266
267 if not 'code' in params:
--> 268 raise MissingCodeError("Missing code parameter in response.")
269
270 if state and params.get('state', None) != state:
MissingCodeError: (missing_code) Missing code parameter in response.