Search code examples
pythonapilinkedin-api

LinkedIn out of network API calls returning "unauthorized" error


I'm trying to access out-of-network profiles using the LinkedIn API, following the instructions from here.
I'm writing my code in Python.

As I understand it, I just add an extra header to my HTTPSConnection request:

normal call:

connection.request(method, relative_url, body = body, headers={'Authorization': OAuth_header})

out-of-network call:

// these values were extracted from the http-header I received
name = x-li-auth-token
value = name:R8Y4
connection.request(method, relative_url, body = body, headers={'Authorization': OAuth_header, name: value})

When I do the out-of-network call, I get an error:

error:
status: 401
timestamp: 1330027911625
request-id: VHUSL0J7TL
error-code: 0
message: [unauthorized]. OAU:k1tofeoqr4id|2dc38f4e-73d1-4d31-9330-dd82aca89616|*01|*01:1330027911:CRc7YYYQRe2VS6woJpGX+qYVa/Q=

I've been testing this on both my own profile and an actual out-of-network profile, no change in error.
From various API requests, the "value" changes slightly, and I have tried all variants:

"name:R8Y4"
"search-name:R8Y4"
"OUT_OF_NETWORK:R8Y4"

I'm guessing it has to do with the HTTP headers, but I have no idea what is wrong.

Please help! Thank you.


Solution

  • I'm not sure why your call is failing. Here's the sequence using the tested oauth2 library in python, including the entire HTTP conversation.

    First, do the search: http://api.linkedin.com/v1/people-search:(people:(distance,id,first-name,last-name,headline,api-standard-profile-request))

    Parameters (I'm using OAuth in the parameters for this call)
    oauth_body_hash=2jmj7l5rSw0yVb%2FvlWAYkK%2FYBwk%3D
    oauth_nonce=41358038
    oauth_timestamp=1330098205
    oauth_consumer_key=xxx
    oauth_signature_method=HMAC-SHA1
    facet=network%2CO
    oauth_version=1.0
    oauth_token=xxx
    keywords=Schneider+Electric
    oauth_signature=xxx
    
    Response includes:
    <person>
      <distance>-1</distance>
      <id>UBAQYFeiHo</id>
      <first-name></first-name>
      <last-name>Private</last-name>
      <headline>Assistant Engineer at Schneider Electric</headline>
      <api-standard-profile-request>
        <url>http://api.linkedin.com/v1/people/UBAQYFeiHo</url>
        <headers total="1">
          <http-header>
            <name>x-li-auth-token</name>
            <value>OUT_OF_NETWORK:wHti</value>
          </http-header>
        </headers>
      </api-standard-profile-request>
     </person>
    

    Second call, to get the profile: http://api.linkedin.com/v1/people/UBAQYFeiHo:(id,first-name,last-name)

    Request headers:
    Host: api.linkedin.com
    x-li-auth-token: OUT_OF_NETWORK:wHti
    accept-encoding: gzip, deflate
    user-agent: Python-httplib2/$Rev$
    
    Response:
    {'status': '200', 'content-length': '158', 'content-location': u'http://api.linkedin.com/v1/people/UBAQYFeiHo:(id,first-name,last-name)?oauth_body_hash=2jmj7l5rSw0yVb%2FvlWAYkK%2FYBwk%3D&oauth_nonce=27886786&oauth_timestamp=1330098212&oauth_consumer_key=xxx&oauth_signature_method=HMAC-SHA1&oauth_version=1.0&oauth_token=xxx&oauth_signature=xxx', 'transfer-encoding': 'chunked', 'vary': '*', 'server': 'Apache-Coyote/1.1', '-content-encoding': 'gzip', 'date': 'Fri, 24 Feb 2012 15:43:34 GMT', 'x-li-request-id': 'N368G241EA', 'x-li-format': 'xml', 'content-type': 'text/xml;charset=UTF-8'}
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <person>
      <id>UBAQYFeiHo</id>
      <first-name></first-name>
      <last-name>Private</last-name>
    </person>
    

    The python code to make the second part work with the oauth2 library is:

    import oauth2 as oauth
    import time
    
    url = "http://api.linkedin.com/v1/people/UBAQYFeiHo:(id,first-name,last-name)"
    consumer = oauth.Consumer(
    key="xxx",
    secret="xxx")
    
    token = oauth.Token(
    key="xxx", 
    secret="xxx")
    
    client = oauth.Client(consumer, token)
    
    resp, content = client.request(url, headers={'x-li-auth-token':'OUT_OF_NETWORK:wHti'})
    print resp
    print content