晚上好,我一直在尝试将我的博客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
有人可以帮我这个忙吗?我真的很感激:)
你好
最后,我用gdata.gauth解决了我的问题: auth2token = gdata.gauth.OAuth2Token(client_id,client_secret,scope,user_agent)
获得授权令牌后,我生成一个授权的URL以获取访问代码 auth2token.generate_authorize_url(redirect_uri=URL,approval_prompt="force").
获得此网址后,您将手动获取代码并生成刷新令牌,并使用该令牌生成访问令牌:token = auth2token.get_access_token(code)
。很简单。有关如何将令牌保存到文件中的Blob字符串的任何其他信息,请参考:gdata-python-api +具有简单身份验证的Analytics
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句