Python Linkedin API OAuth2 HTTP Error 401: Unauthorized

planet260

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

planet260

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

HTTP 401 Unauthorized error occurs in Spring Boot test

Moving to Spring Boot 1.5.1 and OAuth2 + JWT token - Error 401 Unauthorized

Linkedin OAuth2 authorization code error

curl 401 Unauthorized error

HTTPError: HTTP Error 401: Unauthorized for sendgrid integration with python

HTTP Error 401: Unauthorized using urllib.request.urlopen

Web API 2: Random 401 unauthorized on POST

Django Rest Framework: HTTP 401 Unauthorized error

401 unauthorized github api

Google API with GoogleSignIn - Error 401 - unauthorized

Prometheus returned error "server returned HTTP status 401 Unauthorized" for .NET core api

401 Unauthorized error Binance API in r

Julia API Call to Alpaca trading receives HTTP/1.1 401 Unauthorized

unauthorized_scope_error in LinkedIn oAuth2 authentication

Getting error 401 - unauthorized with google API

Kubernetes client python API error 401 - Unauthorized

401 Unauthorized Request Discord API with OAuth

Axios Error 401 (Unauthorized) Error when trying to fetch api

401 Unauthorized on dropbox API

Solr HTTP error: Unauthorized (401)

"HTTP Error 401: Unauthorized" when querying youtube api for playlist with python

HTTP/1.1 401 Unauthorized Error on curl request - heroku

OAuth 1.0 one-legged client "HTTP 401 Unauthorized error" for PATCH method

getting HTTP Status 401 - unauthorized user error for ebayclassifeids Api

Python Tweepy for Twitter API returns 'error 401 : Unauthorized'

The remote server returned an error: (401) Unauthorized from LinkedIn

Jujucharms API error 401 unauthorized

VB.NET 401 Unauthorized error in PayPal OAuth API

Error getting access token for service account: 401 Unauthorized\nPOST https://oauth2.googleapis.com/token; Google Calendar API