1

I am trying to Share a Post on Linkedin on user's behalf. I took the user through authentication process to generate oauth2 token. I have the token but now i am stuck how to use it. All the help i found on the internet was regarding Oauth not Oauth2. I am trying to send the request but i am getting HTTP Error 401: Unauthorized. Below is my code...

import urllib2, cookielib
cookie_jar = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie_jar))
urllib2.install_opener(opener)

xml_request = ..... XML

headers={'Content-Type': 'application/xml'}
url = "http://api.linkedin.com/v1/people/~/shares/?oauth2_access_token=XXXX"
req = urllib2.Request(url, data=xml_request, headers = headers)
rsp = opener.open(req)
content = rsp.read()

I have checked and the token is valid i am getting Network Updates using the same token... I have searched and searched but still no help on Oauth2. All the Linkedin Clients i have seen in using Oauth not Oauth2.. Please help me out on how to send this request. If anyone know any api or client which uses oauth2 please let me know.. Thanks in advance for your help

1 Answer 1

1

I wrote below code to Share Content on linkedin using OAuth 2.0

import requests
import json
def make_request(method, url, token ,data=None, params=None, headers=None, timeout=60):
    headers = {'x-li-format': 'json', 'Content-Type': 'application/json'}
    params = {} 
    kw = dict(data=data, params=params, headers=headers, timeout=timeout)
    params.update({'oauth2_access_token': token})
    return requests.request(method.upper(), url, **kw)   

def submit_share(comment, title, description, submitted_url, submitted_image_url, token):
    post = {
        'comment': comment,
        'content': {
            'title': title,
            'submitted-url': submitted_url,
            'submitted-image-url': submitted_image_url,
            'description': description
        },
        'visibility': {
            'code': 'anyone'
        }
    }
    url = 'https://api.linkedin.com/v1/people/~/shares'
    try:
        response = make_request('POST', url, token,data=json.dumps(post))
        response = response.json()
        return response
    except Exception:
        return False

I hope this code helps anyone. Regards

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.