Skip to content

Commit 91f85df

Browse files
author
Özgür Vatansever
committed
new oauth2 implementation improved
1 parent 5cde3e9 commit 91f85df

File tree

7 files changed

+194
-341
lines changed

7 files changed

+194
-341
lines changed

linkedin/examples/__init__.py

Whitespace-only changes.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from linkedin.linkedin import (LinkedInAuthentication, LinkedInApplication,
2+
PERMISSIONS)
3+
4+
5+
if __name__ == '__main__':
6+
API_KEY = 'wFNJekVpDCJtRPFX812pQsJee-gt0zO4X5XmG6wcfSOSlLocxodAXNMbl0_hw3Vl'
7+
API_SECRET = 'daJDa6_8UcnGMw1yuq9TjoO_PMKukXMo8vEMo7Qv5J-G3SPgrAV0FqFCd0TNjQyG'
8+
RETURN_URL = 'http://localhost:8000'
9+
10+
authentication = LinkedInAuthentication(API_KEY, API_SECRET, RETURN_URL,
11+
PERMISSIONS.enums.values())
12+
print authentication.authorization_url
13+
application = LinkedInApplication(authentication)

linkedin/exceptions.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class BaseLinkedInError(Exception):
2+
pass
3+
4+
5+
class LinkedInHTTPError(BaseLinkedInError):
6+
pass
7+
8+
9+
class LinkedInError(BaseLinkedInError):
10+
def __init__(self, error):
11+
if 'error' in error:
12+
Exception.__init__(self, u'%s: %s' % (error['error'],
13+
error['error_description']))
14+
else:
15+
Exception.__init__(self, u'%s: %s' % ('Request Error',
16+
error['message']))

linkedin/linkedin.py

Lines changed: 88 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
# -*- coding: utf-8 -*-
2-
31
import requests
42
import urllib
53
import random
64
import hashlib
7-
import collections
85
import contextlib
96
import json
107

@@ -13,11 +10,12 @@
1310
except ImportError:
1411
from StringIO import StringIO
1512

16-
import urllib, time, random, httplib, hmac, binascii, cgi, string
17-
from HTMLParser import HTMLParser
13+
from .models import AccessToken, LinkedInInvitation
14+
from .utils import make_enum, to_utf8
15+
from .exceptions import LinkedInError, LinkedInHTTPError
16+
1817

19-
from .model import *
20-
from .utils import make_enum, to_utf8, HTTP_METHODS
18+
__all__ = ['LinkedInAuthentication', 'LinkedInApplication', 'PERMISSIONS']
2119

2220
PERMISSIONS = make_enum('Permission',
2321
BASIC_PROFILE='r_basicprofile',
@@ -41,24 +39,17 @@
4139
JOB_SEARCH='https://api.linkedin.com/v1/job-search')
4240

4341

44-
AccessToken = collections.namedtuple('AccessToken', ['access_token', 'expires_in'],
45-
verbose=False, rename=False)
46-
47-
48-
class BaseLinkedInError(Exception):
49-
pass
50-
51-
class LinkedInHTTPError(BaseLinkedInError):
52-
pass
53-
54-
class LinkedInError(BaseLinkedInError):
55-
def __init__(self, error):
56-
if 'error' in error:
57-
Exception.__init__(self, u'%s: %s' % (error['error'],
58-
error['error_description']))
59-
else:
60-
Exception.__init__(self, u'%s: %s' % ('Request Error',
61-
error['message']))
42+
NETWORK_UPDATES = make_enum('NetworkUpdateType',
43+
APPLICATION='APPS',
44+
COMPANY='CMPY',
45+
CONNECTION='CONN',
46+
JOB='JOBS',
47+
GROUP='JGRP',
48+
PICTURE='PICT',
49+
EXTENDED_PROFILE='PRFX',
50+
CHANGED_PROFILE='PRFU',
51+
SHARED='SHAR',
52+
VIRAL='VIRL')
6253

6354

6455
class LinkedInAuthentication(object):
@@ -268,7 +259,7 @@ def join_group(self, group_id):
268259
response = self.make_request('PUT', url,
269260
data=json.dumps({'membershipState': {'code': 'member'}}))
270261
response.raise_for_status()
271-
except (requests.ConnectionError, HTTPError), error:
262+
except (requests.ConnectionError, requests.HTTPError), error:
272263
raise LinkedInHTTPError(error.message)
273264
else:
274265
return True
@@ -278,7 +269,7 @@ def leave_group(self, group_id):
278269
try:
279270
response = self.make_request('DELETE', url)
280271
response.raise_for_status()
281-
except (requests.ConnectionError, HTTPError), error:
272+
except (requests.ConnectionError, requests.HTTPError), error:
282273
raise LinkedInHTTPError(error.message)
283274
else:
284275
return True
@@ -298,7 +289,7 @@ def submit_group_post(self, group_id, title, summary, submitted_url,
298289
try:
299290
response = self.make_request('POST', url, data=json.dumps(post))
300291
response = response.json()
301-
except (requests.ConnectionError, HTTPError), error:
292+
except (requests.ConnectionError, requests.HTTPError), error:
302293
raise LinkedInHTTPError(error.message)
303294
else:
304295
if not self.request_succeeded(response):
@@ -364,7 +355,7 @@ def follow_company(self, company_id):
364355
try:
365356
response = self.make_request('POST', url, data=json.dumps(post))
366357
response = response.json()
367-
except (requests.ConnectionError, HTTPError), error:
358+
except (requests.ConnectionError, requests.HTTPError), error:
368359
raise LinkedInHTTPError(error.message)
369360
else:
370361
if not self.request_succeeded(response):
@@ -376,7 +367,7 @@ def unfollow_company(self, company_id):
376367
try:
377368
response = self.make_request('DELETE', url)
378369
response.raise_for_status()
379-
except (requests.ConnectionError, HTTPError), error:
370+
except (requests.ConnectionError, requests.HTTPError), error:
380371
raise LinkedInHTTPError(error.message)
381372
else:
382373
return True
@@ -435,11 +426,74 @@ def search_job(self, selectors=None, params=None, headers=None):
435426
raise LinkedInError(response)
436427
return response
437428

429+
def submit_share(self, comment, title, description, submitted_url,
430+
submitted_image_url):
431+
post = {
432+
'comment': comment,
433+
'content': {
434+
'title': title,
435+
'submitted-url': submitted_url,
436+
'submitted-image-url': submitted_image_url,
437+
'description': description
438+
},
439+
'visibility': {
440+
'code': 'anyone'
441+
}
442+
}
443+
url = '%s/~/shares' % ENDPOINTS.PEOPLE
444+
try:
445+
response = self.make_request('POST', url, data=json.dumps(post))
446+
response = response.json()
447+
except (requests.ConnectionError, requests.HTTPError), error:
448+
raise LinkedInHTTPError(error.message)
449+
else:
450+
if not self.request_succeeded(response):
451+
raise LinkedInError(response)
452+
return response
438453

454+
def get_network_updates(self, types, self_scope=True, params=None, headers=None):
455+
url = '%s/~/network/updates' % ENDPOINTS.PEOPLE
456+
if not params:
457+
params = {}
439458

459+
if types:
460+
params.update({'type': types})
440461

441-
if __name__ == '__main__':
442-
authentication = LinkedInAuthentication('wFNJekVpDCJtRPFX812pQsJee-gt0zO4X5XmG6wcfSOSlLocxodAXNMbl0_hw3Vl', 'daJDa6_8UcnGMw1yuq9TjoO_PMKukXMo8vEMo7Qv5J-G3SPgrAV0FqFCd0TNjQyG', 'http://localhost', PERMISSIONS.enums.values())
443-
authentication.token = AccessToken(access_token=u'AQXbxD10Rxxp17Yiru-9MNf3Glb6jxwQJmwkO_ip9x6G-8XA9eiccC0C_re3icyDSUTrU27ckoeusvFmLWdCVL7_Xa32ccdJkj-RiKsWiPWeNRswD5xkuhuGasJKfjnrMFGtUZlG9MVOATT5B-Eg5XFuuoFLMcgVvo-LAe0jgE5Rbj2FqZ0', expires_in=5182360)
462+
if self_scope is True:
463+
params.update({'scope': 'self'})
444464

445-
application = LinkedInApplication(authentication)
465+
try:
466+
response = self.make_request('GET', url, params=params, headers=headers)
467+
response = response.json()
468+
except requests.ConnectionError as error:
469+
raise LinkedInHTTPError(error.message)
470+
else:
471+
if not self.request_succeeded(response):
472+
raise LinkedInError(response)
473+
return response
474+
475+
def get_network_status(self, params=None, headers=None):
476+
url = '%s/~/network/network-stats' % ENDPOINTS.PEOPLE
477+
try:
478+
response = self.make_request('GET', url, params=params, headers=headers)
479+
response = response.json()
480+
except requests.ConnectionError as error:
481+
raise LinkedInHTTPError(error.message)
482+
else:
483+
if not self.request_succeeded(response):
484+
raise LinkedInError(response)
485+
return response
486+
487+
def send_invitation(self, invitation):
488+
assert type(invitation) == LinkedInInvitation, 'LinkedInInvitation required'
489+
url = '%s/~/mailbox' % ENDPOINTS.PEOPLE
490+
try:
491+
response = self.make_request('POST', url,
492+
data=json.dumps(invitation.json))
493+
response = response.json()
494+
except (requests.ConnectionError, requests.HTTPError), error:
495+
raise LinkedInHTTPError(error.message)
496+
else:
497+
if not self.request_succeeded(response):
498+
raise LinkedInError(response)
499+
return response

0 commit comments

Comments
 (0)