晚上好,我一直试图将我的博客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
有人可以帮我解决这个问题吗?我真的很感激它:)
问候
答案 0 :(得分:0)
最后我用gdata.gauth解决了我的问题:
我使用了auth2token = gdata.gauth.OAuth2Token(client_id,client_secret,scope,user_agent)
获取授权令牌后,我会生成一个授权网址,以获取auth2token.generate_authorize_url(redirect_uri=URL,approval_prompt="force").
获得此网址后,您手动获取代码并生成刷新令牌,您可以使用该令牌生成访问令牌:
token = auth2token.get_access_token(code)
。很容易。有关如何将令牌保存到文件中的blob字符串的任何其他信息,请参考以下内容:
gdata-python-api + Analytics with simple auth