1

このコードは、リンクされた接続のマップを実行するためにオンラインで入手できます。これは、リンクされた API を使用します。正常に接続でき、実際にデータを csv に書き込む最後のスクリプトまで、すべて正常に実行されます。

コードを実行するたびに

import oauth2 as oauth
import urlparse
import simplejson
import codecs

CONSUMER_KEY = "xxx"
CONSUMER_SECRET = "xxx"
OAUTH_TOKEN = "xxx"
OAUTH_TOKEN_SECRET = "xxx"

OUTPUT = "linked.csv"

def linkedin_connections():

    # Use your credentials to build the oauth client
consumer = oauth.Consumer(key=CONSUMER_KEY, secret=CONSUMER_SECRET)
token = oauth.Token(key=OAUTH_TOKEN, secret=OAUTH_TOKEN_SECRET)
client = oauth.Client(consumer, token)

# Fetch first degree connections
resp, content = client.request('http://api.linkedin.com/v1/people/~/connections?format=json')
results = simplejson.loads(content)    

# File that will store the results
output = codecs.open(OUTPUT, 'w', 'utf-8')

# Loop thru the 1st degree connection and see how they connect to each other
for result in results["values"]:
    con = "%s %s" % (result["firstName"].replace(",", " "),    result["lastName"].replace(",", " "))
    print >>output, "%s,%s" % ("John Henry",  con)

# This is the trick, use the search API to get related connections
    u = "https://api.linkedin.com/v1/people/%s:(relation-to-viewer:(related-connections))?format=json" % result["id"]
    resp, content = client.request(u)
    rels = simplejson.loads(content)
try:
for rel in rels['relationToViewer']['relatedConnections']['values']:
    sec = "%s %s" % (rel["firstName"].replace(",", " "), rel["lastName"].replace(",", " "))
    print >>output, "%s,%s" % (con, sec)
except:
    pass

if __name__ == '__main__':
    linkedin_connections()     

for result in results["values"]:
    KeyError: 'values'

これを実行すると、次のエラー メッセージが表示されます。

Traceback (most recent call last):

File "linkedin-2-query.py", line 51, in <module>
linkedin_connections()
File "linkedin-2-query.py", line 35, in linkedin_connections
for result in results["values"]:
KeyError: 'values'

どんな提案や助けも大歓迎です!

4

1 に答える 1