Python script on adding connection from linkedin - python

I'm trying to use python-linkedin library here https://github.com/ozgur/python-linkedin
to send an invitation to linkedin contact via script
But on following code:
from linkedin import linkedin
API_KEY = 'wFNJekVpDCJtRPFX812pQsJee-gt0zO4X5XmG6wcfSOSlLocxodAXNMbl0_hw3Vl'
API_SECRET = 'daJDa6_8UcnGMw1yuq9TsjoO_PMKukXMo8vEMo7Qv5J-G3SPgrAV0FqFCd0TNjQyG'
RETURN_URL = 'https://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
application = linkedin.LinkedInApplication(authentication)
I get "The redirect_uri does not match the registered value" when pasting authentication.authorization_url into the browser
Can someone please help ? Feel free to suggest any other methods ;)
Thanks

When you grant access to the application, you will be redirected to the return url with the following query strings appended to your RETURN_URL
Take the key value after http://localhost:8000/?code=
add it after the authentication.authorization_code = "the code provided"
use authentication.get_access_token()
then application = linkedin.LinkedInApplication(token='the token provided')
Quick usage example:
from linkedin import server
application = server.quick_api(KEY, SECRET)
application.get_profile()

Related

Python - "client_id" is missing. How to solve?

I've been playing with the LinkedIn api (OAuth 2) and I've found an example to help test it. I've followed the tutorial to the letter, but for some reason when I provide my full redirect URL (as requested in the code), I get the error: (invalid_request) A required parameter "client_id" is missing. I'm not sure what I'm doing wrong, but if anyone has any idea, I appreciate the feedback.
Upon searching for a solution, I've found another person struggling with this: "client_id" is missing when authenticate with LinkedIn
Here's the code from the example:
Linkedin.py
from requests_oauthlib import OAuth2Session
from requests_oauthlib.compliance_fixes import linkedin_compliance_fix
# Credentials you get from registering a new application
client_id = SECRET
client_secret = SECRET
# OAuth endpoints given in the LinkedIn API documentation
authorization_base_url = 'https://www.linkedin.com/uas/oauth2/authorization'
token_url = 'https://www.linkedin.com/uas/oauth2/accessToken'
linkedin = OAuth2Session(client_id, redirect_uri='http://localhost:8000')
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_response)
# Fetch a protected resource, i.e. user profile
r = linkedin.get('https://api.linkedin.com/v1/people/~')
print (r.content)
Link to example: https://requests-oauthlib.readthedocs.io/en/latest/examples/linkedin.html
Additional Note: The tutorial I used didn't have a date on it. I can only assume the links used in the API tutorial are correct and up to date.
This one is a little old but I thought I'd share some additional changes that need to be made to the LinkedIn example from the oauth requests documentation site.
Along with the updated links, it would seem that LinkedIn is expecting the client_id in the body of the request when trading the verifier code for a a token. I'm not exactly sure where or when it gets left behind but after drilling down in oauth-request source code I found that the fetch method has an argument which forces the client_id to be included in the request body (include_client_id) adding it to the fetch method should make the example work.
linkedin.fetch_token(token_url, client_secret=client_secret,
authorization_response=redirect_response,
include_client_id=True)
The issue is with the URLs, I wrote a similar program and it worked perfectly for me:
from requests_oauthlib import OAuth2Session
from requests_oauthlib.compliance_fixes import linkedin_compliance_fix
# Credentials you get from registering a new application
client_id = '<the client id you get from linkedin>'
client_secret = '<the client secret you get from linkedin>'
redirect_url = '<authorized redirect URL from LinkedIn config>'
# OAuth endpoints given in the LinkedIn API documentation (you can check for the latest 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 configuration)
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_response)
# Fetch a protected resource, i.e. user profile
r = linkedin.get('https://api.linkedin.com/v1/people/~')
print(r.content)
I hope it helps!
While this may not be the reason for your issue, you are using an older version of LinkedIn's authentication URLs. From LinkedIn's OAuth documentation (https://learn.microsoft.com/en-us/linkedin/shared/authentication/authorization-code-flow?context=linkedin/consumer/context) your authorziation_base_url should be
https://www.linkedin.com/oauth/v2/authorization

Python Simple OAuth access

I am trying to access a proprietary web service that has Oauth access. I have no experience with that and I am trying to use ;'requests' and 'requests_oauthlib'.
I have the following access data:
auth_url = 'https://foobar.url.com/oauth/token'
data_url = 'https://foobar.url.com/rest/v1/aname/overview'
API_Key = 'Basic Y2xpY2tleS1hcHA6YXBwLWFjY2Vzcw=='
username ='ausername' password = 'apassword' grant_type = 'password'
The API key should be put in the Header in the Field 'Authorization'
Any examples that fit the above would be appreciated. I cannot find any match with the requests_oauth docs.
Well, requests_oauthlib hasn't provided a way to issue password grant type token. You can try Authlib, the client of Authlib has a similar API, check how to authenticate with password grant type:
https://docs.authlib.org/en/latest/client/oauth2.html#oauth2session-for-password

Getting SoundCloud Access Token with Python

https://github.com/soundcloud/soundcloud-python
I'm using the Python wrapper above, and I am struggling to get the access token.
import soundcloud
client = soundcloud.Client(
client_id=YOUR_CLIENT_ID,
client_secret=YOUR_CLIENT_SECRET,
redirect_uri='http://yourapp.com/callback'
)
redirect(client.authorize_url())
I am able to reach this point and it successfully allows the user to authorize. However I am lost as to how I am supposed to get the access token.
The documentation says the following:
access_token, expires, scope, refresh_token = client.exchange_token(
code=request.args.get('code'))
render_text("Hi There, %s" % client.get('/me').username)
When I use this, it gives me a 500 error.
On redirect client.authorize_url(), the user will be redirected to a SoundCloud connect screen in their browser and asked to authorize their account with your application.
If the user approves the authorization request, they will be sent to the redirect_uri specified in redirect_uri='http://yourapp.com/callback'. From there, you can extract the code parameter from the query string and use it to obtain an access token.
import soundcloud
# create client object with app credentials
client = soundcloud.Client(client_id='YOUR_CLIENT_ID',
client_secret='YOUR_CLIENT_SECRET',
redirect_uri='http://example.com/callback')
# exchange authorization code for access token
code = params['code']
access_token = client.exchange_token(code)
This is straight from the Server-Side Authentication docs.
You could use selenium webdriver
pip install --upgrade selenium
to open a browser window, log in to the soundcloud account and get the code.
#!/usr/bin/env python
# coding=utf-8
import soundcloud
from selenium import webdriver
driver = webdriver.Firefox(
executable_path='<YOUR_PATH_TO>/geckodriver')
CLIENT_ID='<YOUR_CLIENT_ID>'
CLIENT_SECRET='<YOUR_CLIENT_SECRET>'
REDIRECT_URL='<YOUR_REDIRECT_URL>'
client = soundcloud.Client(
client_id=CLIENT_ID,
client_secret=CLIENT_SECRET,
redirect_uri=REDIRECT_URL)
AUTH_URL = client.authorize_url()
driver.get(AUTH_URL) ## open Firefox to access Soundcloud
code = raw_input("WAITING FOR ACCESS... type ENTER")
code = driver.current_url.replace(REDIRECT_URL+'/?code=','')[:-1]
ACCESS_TOKEN = client.exchange_token(code).access_token
USER = client.get('/me').permalink
FILE = "SOUNDCLOUD.%s.access_token" % USER
FILE_W = open(FILE,'w')
FILE_W.write(ACCESS_TOKEN)
FILE_W.close()
driver.quit()
Maybe you'll need to get geckodriver, you can google it to find the one for your OS.
Note that this access_token is not expiring. You don't need a refresh_token.
You can print the full response object with:
from pprint import pprint
[...]
code = driver.current_url.replace(REDIRECT_URL+'/?code=','')[:-1]
TOKEN_OBJECT = client.exchange_token(code)
pprint (vars(TOKEN_OBJECT))
ACCESS_TOKEN = TOKEN_OBJECT.access_token
[...]

Python LinkedIn Search API 403 error

I am trying to get public profiles of people who work in company X to get their title, id, and connection. How do I properly use the Search API so I do not get 403 Forbidden error?
from linkedin import linkedin
CONSUMER_KEY = 'XXX'
CONSUMER_SECRET = 'XXX'
USER_TOKEN = 'XXX'
USER_SECRET = 'XXX'
RETURN_URL = ''
auth = linkedin.LinkedInDeveloperAuthentication(CONSUMER_KEY, CONSUMER_SECRET,
USER_TOKEN, USER_SECRET,
RETURN_URL,
permissions=linkedin.PERMISSIONS.enums.values())
app = linkedin.LinkedInApplication(auth)
mm=app.search_profile(selectors=[{'people': ['headline','id','num-connections',]}], params={'keywords': 'microsoft'})
print mm
So this code gives me a error
Message File Name Line Position
Traceback
<module> <module1> 30
search_profile C:\Python27\lib\site-packages\linkedin\linkedin.py 194
raise_for_error C:\Python27\lib\site-packages\linkedin\utils.py 65
LinkedInForbiddenError: 403 Client Error: Forbidden: Access to people search denied.
Was the search API updated so search cannot be accessed. I do not want to use the normal search and copy paste everything.
So it appears LinkedIn has a vetted process for developer access
http://developer-programs.linkedin.com/forum/error-403-client-error-forbidden-unknown-error
and you might have call customer service and agree to a TOS
http://community.linkedin.com/questions/116784/how-can-i-get-vetted-api-access-to-use-the-people.html
Another option would be to try using selenium to get the data http://www.seleniumhq.org/
Python bindings can be found here:https://selenium-python.readthedocs.org/

Integration of LinkedIn API in Django app

I'm trying to get access token from Linkedin API using the python-linkedin package given at https://github.com/ozgur/python-linkedin using following code
def get_linkedin_token(request):
access_code = request.GET.get('code')
if access_code is None:
request.session['authentication'] = linkedin.LinkedInAuthentication(
LINKEDIN_CONSUMER_KEY,
LINKEDIN_CONSUMER_SECRET,
RETURN_URL,
linkedin.PERMISSIONS.enums.values())
**url = request.session['authentication'].authorization_url**
return HttpResponseRedirect(url)
else:
request.session['authentication'].authorization_code = access_code
access_token = authentication.get_access_token()
return HttpResponse(access_token)
the above code give me an Type error at url ' is not JSON serializable'; when I open the url in browser it works fine but in app its giving error described.
what is going wwrong how can I fix it?
thanks in advance
the above code is right the minor correction needed at the line
access_token = authentication.get_access_token()
it should be
access_token = request.session['authentication'].get_access_token()
because I initialized authentication object as session variable and was calling simply without using request.session[ ]. So after doing so anyone can easily get access token for LinkedIn API using python-linkedin

Categories

Resources