python-linkedin api - how do I use it?

后端 未结 3 788
被撕碎了的回忆
被撕碎了的回忆 2021-01-05 16:37

I know, questions regarding this have been asked before but I can´t find a solution. I Am trying to access my LinkedIn account through the supposedly simple to use python-li

相关标签:
3条回答
  • 2021-01-05 17:19

    It looks like a lot of these answers might be out of date.

    Given it looks like you aren't tied to a specific project, I would recommend you take a look at https://github.com/tomquirk/linkedin-api (disclaimer: I maintain it).

    You can authenticate with LinkedIn with the following:

    from linkedin_api import LinkedIn
    
    # Authenticate using any LinkedIn account credentials
    api = Linkedin('youremail@gmail.com', 'l33tpassword')
    

    After that, you can go ahead and perform requests, for email, get a LinkedIn profile:

    profile = api.get_profile('profile-id')
    
    0 讨论(0)
  • 2021-01-05 17:27

    Updated for Python 3:

    #%%### 3. LinkedIn API Work
    
    import oauth2 as oauth
    import urllib
    
    consumer_key='XXXXXX' #from Linkedin site
    consumer_secret='XXXXXX' #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') #convert binary to utf-8 string
    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)') #prompt for input (y)
    oauth_verifier=input('What is the PIN?') #prompt for 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.")
    

    linkedinapioauth2python

    0 讨论(0)
  • 2021-01-05 17:38

    U only need ClientID and Client_SECRET. The following code shall help u to get other two important keys. The access token keys shall be valid for 60 days. Use ouath2 anyway, The redirect url i choose is 'http://localhost:3000/auth/linkedin/callback'

    check it out

    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."
    
    0 讨论(0)
提交回复
热议问题