forked from LinkedInAttic/api-get-started
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtutorial.py
More file actions
296 lines (230 loc) · 11.6 KB
/
tutorial.py
File metadata and controls
296 lines (230 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# Copyright 2011 LinkedIn Corporation
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import oauth2 as oauth
import httplib2
import time, os, simplejson
import urlparse
import BaseHTTPServer
from xml.etree import ElementTree as ET
# Need to install:
# ElementTree for XML parsing:
# easy_install ElementTree
# http://effbot.org/downloads#elementtree
# simplejson for JSON parsing:
# easy_install simplejson
# https://github.com/simplejson/simplejson
consumer_key = 'xxxxxxxxxxxxxx'
consumer_secret = 'xxxxxxxxxxxxxx'
request_token_url = 'https://api.linkedin.com/uas/oauth/requestToken'
access_token_url = 'https://api.linkedin.com/uas/oauth/accessToken'
authorize_url = 'https://api.linkedin.com/uas/oauth/authorize'
config_file = '.service.dat'
xml_file = '.xml.dat'
http_status_print = BaseHTTPServer.BaseHTTPRequestHandler.responses
def get_auth():
consumer = oauth.Consumer(consumer_key, consumer_secret)
client = oauth.Client(consumer)
try:
filehandle = open(config_file)
except IOError as e:
filehandle = open(config_file,"w")
print("We don't have a service.dat file, so we need to get access tokens!");
content = make_request(client,request_token_url,{},"Failed to fetch request token","POST")
request_token = dict(urlparse.parse_qsl(content))
print "Go to the following link in your browser:"
print "%s?oauth_token=%s" % (authorize_url, request_token['oauth_token'])
oauth_verifier = raw_input('What is the PIN? ')
token = oauth.Token(request_token['oauth_token'],
request_token['oauth_token_secret'])
token.set_verifier(oauth_verifier)
client = oauth.Client(consumer, token)
content = make_request(client,access_token_url,{},"Failed to fetch access token","POST")
access_token = dict(urlparse.parse_qsl(content))
token = oauth.Token(
key=access_token['oauth_token'],
secret=access_token['oauth_token_secret'])
client = oauth.Client(consumer, token)
simplejson.dump(access_token,filehandle)
else:
config = simplejson.load(filehandle)
if ("oauth_token" in config and "oauth_token_secret" in config):
token = oauth.Token(config['oauth_token'],
config['oauth_token_secret'])
client = oauth.Client(consumer, token)
else:
print("We had a .service.dat file, but it didn't contain a token/secret?")
exit()
return client
# Simple oauth request wrapper to handle responses and exceptions
def make_request(client,url,request_headers={},error_string="Failed Request",method="GET",body=None):
if body:
resp,content = client.request(url, method, headers=request_headers, body=body)
else:
resp,content = client.request(url, method, headers=request_headers)
print resp.status
if resp.status >= 200 and resp.status < 300:
return content
elif resp.status >= 500 and resp.status < 600:
error_string = "Status:\n\tRuh Roh! An application error occured! HTTP 5XX response received."
log_diagnostic_info(client,url,request_headers,method,body,resp,content,error_string)
else:
status_codes = {403: "\n** Status:\n\tA 403 response was received. Usually this means you have reached a throttle limit.",
401: "\n** Status:\n\tA 401 response was received. Usually this means the OAuth signature was bad.",
405: "\n** Status:\n\tA 405 response was received. Usually this means you used the wrong HTTP method (GET when you should POST, etc).",
400: "\n** Status:\n\tA 400 response was received. Usually this means your request was formatted incorrectly or you added an unexpected parameter.",
404: "\n** Status:\n\tA 404 response was received. The resource was not found."}
if resp.status in status_codes:
log_diagnostic_info(client,url,request_headers,method,body,resp,content,status_codes[resp.status])
else:
log_diagnostic_info(client,url,request_headers,method,body,resp,content,http_status_print[resp.status][1])
def get_xml():
# In python, the easiest XML library prints most elegantly to files
# and not strings. In this case we'll create a temporary file with
# the XML if one doesn't already exist
try:
filehandle = open(xml_file)
except IOError as e:
# fields used in all share examples
# these would usually be provided by you / the user
comment_text = "Testing out the LinkedIn REST Share API with XML";
title_text = "Survey: Social networks top hiring tool - San Francisco Business Times";
url = "http://sanfrancisco.bizjournals.com/sanfrancisco/stories/2010/06/28/daily34.html";
image = "http://images.bizjournals.com/travel/cityscapes/thumbs/sm_sanfrancisco.jpg";
visibility = "anyone";
share_element = ET.Element("share")
comment = ET.SubElement(share_element,"comment")
comment.text = comment_text
content_element = ET.SubElement(share_element,"content")
title = ET.SubElement(content_element,"title")
title.text = title_text
submitted_url = ET.SubElement(content_element,"submitted-url")
submitted_url.text = url
submitted_image_url = ET.SubElement(content_element,"submitted-image-url")
submitted_image_url.text = image
visibility_element = ET.SubElement(share_element,"visibility")
code_element = ET.SubElement(visibility_element,"code")
code_element.text = visibility
tree = ET.ElementTree(share_element)
tree.write(xml_file,encoding="utf-8",xml_declaration=True)
filehandle = open(xml_file)
xml_content = filehandle.read()
return xml_content
def get_json():
comment_text = "Testing out the LinkedIn REST Share API with JSON";
title_text = "Survey: Social networks top hiring tool - San Francisco Business Times";
url = "http://sanfrancisco.bizjournals.com/sanfrancisco/stories/2010/06/28/daily34.html";
image = "http://images.bizjournals.com/travel/cityscapes/thumbs/sm_sanfrancisco.jpg";
visibility = "anyone";
share_object = {
"comment":comment_text,
"content": {
"title":title_text,
"submitted_url":url,
"submitted_image_url":image
},
"visibility": {
"code":visibility
}
}
json_content = simplejson.dumps(share_object)
return json_content
def log_diagnostic_info(client,url,request_headers,method,body,resp,content,error_string):
# we build up a string, then log it, as multiple calls to () are not guaranteed to be contiguous
log = "\n\n[********************LinkedIn API Diagnostics**************************]\n\n"
log += "\n|-> Status: " + str(resp.status) + " <-|"
log += "\n|-> " + simplejson.dumps(error_string) + " <-|"
log += "\n|-> Key: " + consumer_key + " <-|"
log += "\n|-> URL: " + url + " <-|"
log += "\n\n[*****Sent*****]\n";
log += "\n|-> Headers:" + simplejson.dumps(request_headers) + " <-|"
if (body):
log += "\n|-> Body: " + body + " <-|"
log += "\n|-> Method: " + method + " <-|"
log += "\n\n[*****Received*****]\n"
log += "\n|-> Response object: " + simplejson.dumps(resp) + " <-|"
log += "\n|-> Content: " + content + " <-|";
log += "\n\n[******************End LinkedIn API Diagnostics************************]\n\n"
print log
if __name__ == "__main__":
# Get authorization set up and create the OAuth client
client = get_auth()
##############################
# Getting Data from LinkedIn #
##############################
# Simple profile call
print "\n********A basic user profile call********"
response = make_request(client,"http://api.linkedin.com/v1/people/~")
print response
# Simple profile call, returned in JSON
print "\n********Get the profile in JSON********"
response = make_request(client,"http://api.linkedin.com/v1/people/~",{"x-li-format":'json'})
print response
# Simple profile call, returned in JSON, using query param instead of header
print "\n********Get the profile in JSON********"
response = make_request(client,"http://api.linkedin.com/v1/people/~?format=json")
print response
# Simple connections call
print "\n********Get the profile in JSON using query parameter********"
response = make_request(client,"http://api.linkedin.com/v1/people/~/connections")
print response
# Simple connections call
print "\n********Get only 10 connections - using parameters********"
response = make_request(client,"http://api.linkedin.com/v1/people/~/connections?count=10")
print response
# Get network updates that are shares or connection updates
print "\n********GET network updates that are CONN and SHAR********"
response = make_request(client,"http://api.linkedin.com/v1/people/~/network/updates?type=SHAR&type=CONN")
print response
# People search using facets and encoding input parameters
print "\n********People Search using facets and Encoding input parameters i.e. UTF8********"
response = make_request(client,"http://api.linkedin.com/v1/people-search:(people:(first-name,last-name,headline),facets:(code,buckets))?title=D%C3%A9veloppeur&facet=industry,4&facets=location,industry")
print response
############################
# Writing Data to LinkedIn #
############################
xml_content = get_xml()
json_content = get_json()
print "\n********Write to the share - using XML********"
api_url = "http://api.linkedin.com/v1/people/~/shares";
#response = make_request(client,api_url,{"Content-Type":"text/xml"},"Failed to post share","POST",xml_content)
print "\n********Write to the share - using XML, also to twitter********"
api_url = "http://api.linkedin.com/v1/people/~/shares?twitter-post=true";
#response = make_request(client,api_url,{"Content-Type":"text/xml"},"Failed to post share","POST",xml_content)
print "\n********Write to the share - using JSON********"
api_url = "http://api.linkedin.com/v1/people/~/shares";
response = make_request(client,api_url,{'Content-Type':'application/json'},"Failed to post share","POST",json_content)
exit();
############################
# Field Selectors #
############################
print "\n********A basic user profile call with field selectors********"
api_url = "http://api.linkedin.com/v1/people/~:(first-name,last-name,positions)"
response = make_request(client,api_url)
print response
print "\n********A basic user profile call with field selectors going into a subresource********"
api_url = "http://api.linkedin.com/v1/people/~:(first-name,last-name,positions:(company:(name)))"
response = make_request(client,api_url)
print response
print "\n********A basic user profile call into a subresource return data in JSON********"
api_url = "https://api.linkedin.com/v1/people/~/connections:(first-name,last-name,headline)?format=json"
response = make_request(client,api_url)
print response
print "\n********A more complicated example using postings into groups********"
api_url = "http://api.linkedin.com/v1/groups/3297124/posts:(id,category,creator:(id,first-name,last-name),title,summary,creation-timestamp,site-group-post-url,comments,likes)"
response = make_request(client,api_url)
print response
####################################################################################
# Understanding the response, creating logging and response headers #
####################################################################################
print "\n********A basic user profile call and response dissected********"
api_url = "https://api.linkedin.com/v1/people/~";
resp,content = client.request(api_url)
print "\n** Response Headers:\n%s\n" % (resp)