Hi all,
New member here. Please forgive me for my immaturity with Python; I have only just started using the language for a project at University so I might sound entirely ineducate!
I am attempting to use the python REST API to store my connections' data and analyse it using visualisation techniques. The problem is, I can't seem to be able to pass the oAuth dance correctly, possibly because not only have I not understood the details that need to be placed in my code, but also because I've never done it before!
My code is as follows:
# -*- coding: utf-8 -*-
import os
import sys
import webbrowser
import cPickle
from linkedin import linkedin
# Parses out oauth_verifier parameter from window.location.href and
# displays it for the user
RETURN_URL = 'http://miningthesocialweb.appspot.com/static/linkedin_oauth_helper.html'
def oauthDance(key, secret, return_url):
api = linkedin.LinkedIn('mykey', 'mysecret',' ', )
result = api.requestToken()
print result
if not result:
print >> sys.stderr, api.requestTokenError()
return None
authorize_url = api.getAuthorizeURL()
webbrowser.open(authorize_url)
oauth_verifier = raw_input('PIN number, bro: ')
result = api.accessToken(verifier=oauth_verifier)
if not result:
print >> sys.stderr, 'Error: %s\nAborting' % api.getRequestTokenError()
return None
return api
# First, do the oauth_dance
api = oauthDance('mykey', 'mysecret', ' ', )
# Now do something like get your connections:
if api:
connections = api.GetConnections()
else:
print >> sys.stderr, 'Failed to aunthenticate. You need to learn to dance'
sys.exit(1)
# Be careful - this type of API usage is "expensive".
# See http://developer.linkedin.com/docs/DOC-1112
print >> sys.stderr, 'Fetching extended connections...'
extended_connections = [api.GetProfile(member_id=c.id, url=None, fields=[
'first-name',
'last-name',
'current-status',
'educations',
'specialties',
'interests',
'honors',
'positions',
'industry',
'summary',
'location',
]) for c in connections]
# Store the data
if not os.path.isdir('out'):
os.mkdir('out')
f = open('out/linkedin_connections.pickle', 'wb')
cPickle.dump(extended_connections, f)
f.close()
print >> sys.stderr, 'Data pickled to out/linkedin_connections.pickle'
This is based on an excerpt from O' Reilly's 'mining the social web' so it should work. I have installed python-linkedin and I know it's a 3rd party plugin that isn't strictly supported, but if I can manage the login, I should be able to move ahead afterwards. Any halp would be fantastic!
I shall suck my thumbs till then.
- Log in to post comments
No worries, OAuth is tricky stuff. We do have some python examples in our Quick Start Guide, and you can use that to get started - it uses the oauth2 library and you can start from there. Once you've done the dance you'll have a token and secret you can use for development.