我尝试使用oath2与Anaconda Python 2.7.6连接到LinkedIn。 我不确定是什么错误。
我是否需要向/Users/davidlaxer/anaconda/lib/python2.7/site-packages/httplib2/cacerts.txt添加证书
这是错误:
GetDistanceTo
这是我的代码: #!/ usr / bin / env python #encoding:utf-8 """ linkedin-1-oauth.py
/Users/davidlaxer/anaconda/bin/python2 /Users/davidlaxer/workspace/LinkedInGraph/Authorization.py
Traceback (most recent call last):
File "/Users/davidlaxer/workspace/LinkedInGraph/Authorization.py", line 79, in <module>
l.dance()
File "/Users/davidlaxer/workspace/LinkedInGraph/Authorization.py", line 71, in dance
self.request_token()
File "/Users/davidlaxer/workspace/LinkedInGraph/Authorization.py", line 37, in request_token
resp, content = client.request(request_token_url, "POST")
File "/Users/davidlaxer/anaconda/lib/python2.7/site-packages/oauth2/__init__.py", line 682, in request
connection_type=connection_type)
File "/Users/davidlaxer/anaconda/lib/python2.7/site-packages/httplib2/__init__.py", line 1570, in request
(response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
File "/Users/davidlaxer/anaconda/lib/python2.7/site-packages/httplib2/__init__.py", line 1317, in _request
(response, content) = self._conn_request(conn, request_uri, method, body, headers)
File "/Users/davidlaxer/anaconda/lib/python2.7/site-packages/httplib2/__init__.py", line 1252, in _conn_request
conn.connect()
File "/Users/davidlaxer/anaconda/lib/python2.7/site-packages/httplib2/__init__.py", line 1044, in connect
raise SSLHandshakeError(e)
httplib2.SSLHandshakeError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:581)
Process finished with exit code 1
我也尝试过使用python-linkedin包获取凭证 http://localhost:8080/
Created by Thomas Cabrol on 2012-12-03.
Copyright (c) 2012 dataiku. All rights reserved.
Doing the oauth dance to get your LinkedIn token
This is taken from :
http://developer.linkedin.com/documents/authentication
"""
import oauth2 as oauth
import urlparse
import httplib2
consumer_key = "xxx"
consumer_secret = "yyy"
class LinkedIn(object):
def __init__(self, consumer_key, consumer_secret):
self.consumer_key = consumer_key
self.consumer_secret = consumer_secret
self.http = httplib2.Http(ca_certs = '/Users/davidlaxer/Downloads/DigiCertHighAssuranceEVRootCA.pem')
# credentials = self.flow.step2_exchange(code, self.http)
# self.http = credentials.authorize(self.http)
def request_token(self):
self.consumer = oauth.Consumer(consumer_key, consumer_secret)
client = oauth.Client(self.consumer)
request_token_url = 'https://api.linkedin.com/uas/oauth/requestToken?scope=r_network'
resp, content = client.request(request_token_url, "POST")
if resp['status'] != '200':
raise Exception("Invalid response %s." % resp['status'])
self.request_token = dict(urlparse.parse_qsl(content))
print "Request Token:"
print " - oauth_token = %s" % self.request_token['oauth_token']
print " - oauth_token_secret = %s" % self.request_token['oauth_token_secret']
print
def authorize(self):
authorize_url = 'https://api.linkedin.com/uas/oauth/authorize'
print "Go to the following link in your browser:"
print "%s?oauth_token=%s" % (authorize_url, self.request_token['oauth_token'])
print
accepted = 'n'
while accepted.lower() == 'n':
accepted = raw_input('Have you authorized me? (y/n) ')
self.oauth_verifier = raw_input('What is the PIN? ')
def access(self):
access_token_url = 'https://api.linkedin.com/uas/oauth/accessToken'
token = oauth.Token(self.request_token['oauth_token'], self.request_token['oauth_token_secret'])
token.set_verifier(self.oauth_verifier)
client = oauth.Client(self.consumer, token)
resp, content = client.request(access_token_url, "POST")
self.access_token = dict(urlparse.parse_qsl(content))
print "Access Token:"
print " - oauth_token = %s" % self.access_token['oauth_token']
print " - oauth_token_secret = %s" % self.access_token['oauth_token_secret']
print
print "You may now access protected resources using the access tokens above."
print
def dance(self):
self.request_token()
self.authorize()
self.access()
if __name__ == '__main__':
l = LinkedIn(consumer_key, consumer_secret)
l.dance()