Python-linkedin api - how to use it?

I know the questions regarding this have been asked before, but I cannot find a solution. I am trying to access my LinkedIn account through a supposedly easy-to-use python-linkedin library, but cannot do this. According to the Ozgur page https://github.com/ozgur/python-linkedin I need to open the link generated by the .authorization_url function, but this does not work, because I get that my redirect link is incorrect, although I entered it in my application on the developers page LinkedIn That is, when you try to open the link that the .authorization_url function provides, what appears in the browser is the following error message:

"invalid redirect_uri. This value must match the URL registered with the API key.

What I expect is a page on which I can approve access to my account. Can I, as in the code below, specify localhost: 8000 (as the Ozgur page shows) as a redirect link or what kind of connection should it be? Could it be?

from linkedin import linkedin import webbrowser API_KEY = '********' API_SECRET = '*******' RETURN_URL = 'http://localhost:8000' authentication = linkedin.LinkedInAuthentication(API_KEY, API_SECRET, RETURN_URL, linkedin.PERMISSIONS.enums.values()) print (authentication.authorization_url) # open this url on your browser webbrowser.open(authentication.authorization_url) application = linkedin.LinkedInApplication(authentication) authentication.authorization_code = '4CqONljAz622ZBo0' authentication.get_access_token() 

How to do it?

Another question: the above applies to the use of Oauth2, but you can still use Oauth1 in accordance with the developers page and not yet obsolete. However, to use Oauth1, you need four different keys, which are mostly mentioned:

CONSUMER_KEY, CONSUMER_SECRET, USER_TOKEN, USER_SECRET

However, on the application page (i.e. LinkedIn, where the application is registered), I can only find two: ClientID and Client_SECRET, which are for Oauth2. Does this mean that using oauth1 is impossible anyway?

+4
python linkedin
source share
2 answers

U needs only ClientID and Client_SECRET . The following code will help you get two more important keys. Access token keys are valid for 60 days. To use ouath2 anyway, the redirect URL that I choose is ' http: // localhost: 3000 / auth / linkedin / callback '

check

 import oauth2 as oauth import urlparse consumer_key = "******" consumer_secret = "******" 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']) print content print "\n" request_token = dict(urlparse.parse_qsl(content)) print "Requesr Token:", "\n" print "- oauth_token = %s" % request_token['oauth_token'], "\n" print "- oauth_token_secret = %s" % request_token['oauth_token_secret'], "\n" authorize_url = 'https://api.linkedin.com/uas/oauth/authorize' print "Go to the following link in your browser:", "\n" print "%s?oauth_token=%s" % (authorize_url, request_token['oauth_token']), "\n" accepted = 'n' while accepted.lower() == 'n': accepted = raw_input('Have you authorized me? (y/n) ') oauth_verifier = raw_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") access_token = dict(urlparse.parse_qsl(content)) print "Access Token:", "\n" print "- oauth_token = %s" % access_token['oauth_token'], "\n" print "- oauth_token_secret = %s" % access_token['oauth_token_secret'] print "You may now access protected resources using the access tokens above." 
+8
source share

Updated for Python 3:

  # %% ### 3.   LinkedIn API  oauth2  oauth  urllib consumer_key = 'XXXXXX' #   Linkedin consumer_secret = 'XXXXXX' #   Linkedin consumer = oauth.Consumer(consumer_key, consumer_secret)  = oauth.Client() request_token_url = 'https://api.linkedin.com/uas/oauth/requestToken' resp, content = client.request(request_token_url, "POST" )  resp ['status']!= '200':   raise Exception ( " % s." % resp ['status']) content_utf8 = str (content, 'utf-8') #convert     utf-8 request_token =  (urllib.parse.parse_qsl (content_utf8)) authorize_url = request_token [ 'xoauth_request_auth_url'] print ( "      :", "\n" )  (authorize_url + '? oauth_token =' + request_token [ 'oauth_token'])  = "" while accepted.lower() == 'n':    =  ( "  ? (y/n)" ) #prompt   (y) oauth_verifier =  ( "  PIN-?" ) #prompt   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(, ) resp, content = client.request(access_token_url, "POST" ) content8 =  (, 'UTF-8') access_token = dict (urllib.parse.parse_qsl (content8)) print ( " :", "\n" ) print ( "- oauth_token =" + access_token ['oauth_token'] + '\n') print ( "- oauth_token_secret =" + access_token ['oauth_token_secret']) print ( "            " ). > 

linkedin api oauth2 python

0
source share

All Articles