【问题标题】:LinkedIn API Python Key Error 2.7LinkedIn API Python 密钥错误 2.7
【发布时间】:2013-04-14 14:07:40
【问题描述】:

此代码可在线获取,用于在linkedin 中运行您的连接图 这使用了linkedin 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'

任何建议或帮助将不胜感激!

【问题讨论】:

  • 您的网址中有许多空格,这是故意的还是复制粘贴错误?
  • 显然,JSON 响应没有values 键;我怀疑您可能正在寻找 connections
  • -嗨 Martijn,来源没有空格,感谢您在我的上面发现。
  • 查看返回内容的最简单方法是打印result.keys();这对 API 调用结果有何影响?
  • @MartijnPieters 您是否建议在 for 循环中将“值”一词更改为“连接”?

标签: python json csv


【解决方案1】:

我在Visualizing your LinkedIn graph using Gephi – Part 1 的帖子中遇到了同样的问题。

每当请求 dict() 对象(使用格式 a = adict[key])并且键不在字典中时,Python 都会引发 KeyError。 KeyError - Python Wiki

经过一番搜索并添加了一些打印语句后,我意识到我的 OAuth 会话已过期,因此我的 linkedin-2-query.py 脚本中的 OAuth 令牌不再有效。

由于 OAuth 令牌无效,LinkedIn API 不会像脚本预期的那样返回带有键 "values" 的字典。相反,API 返回字符串 'N'。 Python 尝试在字符串'N' 中查找dict 键"values",失败,并生成KeyError: 'values'

所以一个新的、有效的 OAuth 令牌和秘密应该让 API 返回一个包含连接数据的字典。


我再次运行 linkedin-1-oauth.py 脚本,然后访问 LinkedIn Application details page 以查找我的新 OAuth 令牌。 (屏幕截图省略了我的应用程序的值。您应该看到每个 Key、Token 和 Secret 的字母数字值。)

...

然后,我使用新的 OAuth 用户令牌OAuth 用户密码

更新我的 linkedin-2-query.py 脚本
OAUTH_TOKEN = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" # your updated OAuth User Token
OAUTH_TOKEN_SECRET = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" # your updated OAuth User Secret

更新 OAuth 令牌和密码后,我立即运行我的 linkedin-2-query.py 脚​​本。万岁,它运行没有错误,并从 API 检索我的连接数据。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-05
    • 1970-01-01
    • 1970-01-01
    • 2013-08-04
    • 2016-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多