web-dev-qa-db-ja.com

pythonでLinkedin APIを使用する方法

たくさんの方法を試しましたが、どれもうまくいかないようです。 Pythonを使用してLinkedinと接続するのを手伝ってください。私はすべてのトークンを持っています。 python 2.7.5。接続を確立してユーザー名を取得する基本コードのサンプルを投稿してください。

以下では、上記の例のようにキャラクターごとにキャラクターを作成しましたが、機能しません。

https://github.com/ozgur/python-linkedin <---これは私がapiを取得し、正確にコピーした場所です。下記参照:

CONSUMER_KEY = '9puxXXXXXXXX'     # This is api_key
CONSUMER_SECRET = 'brtXoXEXXXXXXXX'   # This is secret_key

USER_TOKEN = '27138ae8-XXXXXXXXXXXXXXXXXXXXXXXXXXX'   # This is oauth_token
USER_SECRET = 'ca103e23XXXXXXXXXXXXXXXXXXXXXXXXXXX'   # This is oauth_secret


from linkedin import linkedin

# Define CONSUMER_KEY, CONSUMER_SECRET,  
# USER_TOKEN, and USER_SECRET from the credentials 
# provided in your LinkedIn application

# 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)

# Use the app....

application.get_profile()

私はこのエラーを受け取ります:

Traceback (most recent call last):
  File "C:/Documents and Settings/visolank/Desktop/Python/programs/linkedinapi.py", line 8, in <module>
    from linkedin import linkedin
  File "C:/Documents and Settings/visolank/Desktop/Python/programs\linkedin\linkedin.py", line 2, in <module>
    from requests_oauthlib import OAuth1
  File "C:\Python27\lib\site-packages\requests_oauthlib\__init__.py", line 1, in <module>
    from .core import OAuth1
  File "C:\Python27\lib\site-packages\requests_oauthlib\core.py", line 4, in <module>
from oauthlib.oauth1 import (Client, SIGNATURE_HMAC, SIGNATURE_TYPE_AUTH_HEADER)
  File "C:\Python27\lib\site-packages\requests_oauthlib\oauthlib\oauth1\__init__.py", line 12, in <module>
    from .rfc5849 import Client
  File "C:\Python27\lib\site-packages\requests_oauthlib\oauthlib\oauth1\rfc5849\__init__.py", line 26, in <module>
    from oauthlib.common import Request, urlencode, generate_nonce
ImportError: No module named oauthlib.common
15
user1681664

とった。今後の参照のために、ここからoauthlibをダウンロードする必要があります https://github.com/idan/oauthlib

ここに完全な機能コードがあります:

CONSUMER_KEY = '9pux1XcwXXXXXXXXXX'     # This is api_key
CONSUMER_SECRET = 'brtXoXEXXXXXXXXXXXXX'   # This is secret_key

USER_TOKEN = '27138ae8-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXb'   # This is oauth_token
USER_SECRET = 'ca103e23-XXXXXXXXXXXXXXXXXXXXXXXX7bba512625e'   # This is oauth_secret
RETURN_URL = 'http://localhost:8000'

from linkedin import linkedin
from oauthlib import *

# Define CONSUMER_KEY, CONSUMER_SECRET,  
# USER_TOKEN, and USER_SECRET from the credentials 
# provided in your LinkedIn application

# 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)

# Use the app....

g = application.get_profile()
print g
11
user1681664

このような方法でUSER_TOKENとUSER_SECRETを取得できます(CONSUMER_KEYとCONSUMER_SECRETがある場合)。

import oauth2 as oauth
import urllib

consumer_key = '' #from Linkedin site
consumer_secret = '' #from Linkedin site
consumer = oauth.Consumer(consumer_key, consumer_secret)
client = oauth.Client(consumer)

request_token_url = 'https://api.linkedin.com/uas/oauth/requestToken'
resp, content = client.request(request_token_url, "POST")
if resp['status'] != '200' :
    raise Exception('Invalid response %s.' % resp['status'])
content_utf8 = str(content,'utf-8')
request_token = dict(urllib.parse.parse_qsl(content_utf8))
authorize_url = request_token['xoauth_request_auth_url']

print('Go to the following link in your browser:', "\n")
print(authorize_url + '?oauth_token=' + request_token['oauth_token'])

accepted='n'
while accepted.lower() == 'n' :
    accepted = input('Have you authorized me? (y/n)')
oauth_verifier = input('What is the PIN?')

access_token_url = 'https://api.linkedin.com/uas/oauth/accessToken'
token = oauth.Token(request_token['oauth_token'], request_token['oauth_token_secret'])
token.set_verifier(oauth_verifier)
client = oauth.Client(consumer, token)
resp, content = client.request(access_token_url, 'POST')
content8 = str(content,'utf-8')
access_token = dict(urllib.parse.parse_qsl(content8))

print('Access Token:', "\n")
print('- oauth_token        = ' + access_token['oauth_token']+'\n')
print('- oauth_token_secret = ' + access_token['oauth_token_secret'])
print('You may now access protected resources using the access tokens above.')
0
Gleb Kosteiko