Python LinkedIn Search API 403 error - python

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/

Related

3-legged authentication with Twitter API and Tweepy in Python problem

Here is the context of what I want to do:
Automate following certain profiles on Twitter with an account that is different from my Twitter developer account. For that I read that I need 3-legged OAuth.
Here is my code:
import tweepy
consumer_key = "XXX"
consumer_secret = "XXX"
oauth1_user_handler = tweepy.OAuth1UserHandler(
consumer_key, consumer_secret,
callback="callback url"
)
print(oauth1_user_handler.get_authorization_url(signin_with_twitter=True))
Following the link printed I am able to authenticate my app using the Twitter account and then get something like this:
https://my_callback_url?oauth_token=XXX&oauth_verifier=XXXX
Following Tweepy's documentation, I should be able to put this oauth_verifier there:
access_token, access_token_secret = oauth1_user_handler.get_access_token(
"Verifier (oauth_verifier) here"
)
However, it is not correct, because every time I run my code I need to authenticate again and get a new oauth_verifier token.
On the Twitter developer portal everything is setup with this OAuth 1 permission and putting a callback URL and website URL was mandatory. I don't know what else to do.
That's where I am stuck.
I am following Tweepy's documentation here
You should be able to reuse the access token and secret, not the verifier.

Python script on adding connection from linkedin

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

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
[...]

how to renew access token with gdata python

I'm trying to switch my picasa app to oauth2.0 .
from gdata.gauth import OAuth2Token
token = OAuth2Token(
client_id=client_id,
client_secret=client_secret,
scope=scope,
user_agent=useragent)
url = token.generate_authorize_url(redirect_uri='urn:ietf:wg:oauth:2.0:oob', approval_prompt='force', access_type="offline")
print url
code = raw_input('code:')
token.get_access_token(code)
And then, I get access token
access_token = token.access_token
When I want to get albums :
albums = gd_client.GetFeed("https://picasaweb.google.com/data/feed/api/user/default?access_token="+access_token)
print albums
Here's the problem. The access token will expired in short time and I'll get error message
(403) Token invalid - Invalid token: Token not found -- Forbidden
I tried many way but no luck.
Please help me to fix this. Thanks

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