【问题标题】:Having trouble trying to use gdata and oauth2 in python尝试在 python 中使用 gdata 和 oauth2 时遇到问题
【发布时间】:2015-08-11 17:27:27
【问题描述】:

晚上好,我一直在尝试将我的博客 python 应用程序迁移到 oauth2,因为旧的 Clientlogin() 已被弃用和删除。 所以,基本上我搜索了整个网络,但无法让我的应用程序正常工作。

这是我用于测试的基本代码:

FLOW =         flow_from_clientsecrets('/home/b/client_secret.json',scope='https://www.googleapis.com/auth/blogger',message="Client Secrets Not Found")

storage = Storage('blogger.dat')
credentials = storage.get()
parser = argparse.ArgumentParser(parents=[tools.argparser])
flags = parser.parse_args()
if credentials is None or credentials.invalid:
    credentials = run_flow(FLOW, storage, flags)
if credentials.access_token_expired:
    credentials.refresh(httplib2.Http())

SCOPE = 'https://www.blogger.com/feeds'

token = gdata.gauth.OAuth2TokenFromCredentials(credentials)

client = gdata.blogger.client.BloggerClient()

token.authorize(client)

post = client.add_post(blog_id, title="blah", body="blah",     labels="label", draft=False, title_type="xhtml", body_type="html")

我收到一个 401 错误代码,每次我尝试这样做时都未经授权。

Traceback (most recent call last):
  File "/home/b/.eclipse/org.eclipse.platform_4.4.2_1473617060_linux_gtk_x86_64/plugins/org.python.pydev_4.0.0.201504132356/pysrc/pydevd.py", line 2278, in <module>
    globals = debugger.run(setup['file'], None, None)
  File "/home/b/.eclipse/org.eclipse.platform_4.4.2_1473617060_linux_gtk_x86_64/plugins/org.python.pydev_4.0.0.201504132356/pysrc/pydevd.py", line 1704, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/home/b/workspace/BloggerPy/simpleblogger.py", line 53, in <module>
    post = client.add_post(blog_id, title="hola", body="holaaa", labels="label", draft=False, title_type="xhtml", body_type="html", token=token)
  File "/usr/local/lib/python2.7/dist-packages/gdata/blogger/client.py", line 111, in add_post
    return self.post(new_entry, BLOG_POST_URL % blog_id, auth_token=auth_token, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/gdata/client.py", line 690, in post
    desired_class=desired_class, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/gdata/client.py", line 298, in request
    **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/gdata/client.py", line 307, in request
    response, Unauthorized)
gdata.client.Unauthorized: Unauthorized - Server responded with: 401, User does not have permission to create new post

有人可以帮我解决这个问题吗?我真的很感激:)

问候

【问题讨论】:

    标签: python eclipse oauth-2.0 gdata


    【解决方案1】:

    最后我用 gdata.gauth 解决了我的问题: 我用auth2token = gdata.gauth.OAuth2Token(client_id,client_secret,scope,user_agent)

    获得授权令牌后,我生成一个授权 url 以获取带有 auth2token.generate_authorize_url(redirect_uri=URL,approval_prompt="force"). 的访问代码

    一旦你得到这个 url,你手动获取代码并生成一个刷新令牌,你用它来生成一个访问令牌: token = auth2token.get_access_token(code)。很容易。有关如何将令牌保存到文件中的 blob 字符串的任何其他信息,请参见参考: gdata-python-api + Analytics with simple auth

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-20
      相关资源
      最近更新 更多