在python中尝试使用gdata和oauth2时遇到麻烦 [英] Having trouble trying to use gdata and oauth2 in python

查看:178
本文介绍了在python中尝试使用gdata和oauth2时遇到麻烦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

晚上,我一直在尝试将我的博主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()
如果凭证为None或credentials.invalid:
credentials = run_flow(FLOW,标志)
如果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错误代码,每次我尝试执行此操作都是未经授权的。

 追溯(最近的最后一次呼叫):
文件/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 ,第2278行,< module>
globals = debugger.run(setup ['file'],None,None)
文件/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,第1704行,运行
pydev_imports.execfile(file,globals,locals)#执行脚本
文件/ home / b / workspace / BloggerPy /simpleblogger.py,第53行,位于< module>
post = client.add_post(blog_id,title =hola,body =holaaa,labels =label,draft = False,title_type =xhtml,body_type =html,token = token)
文件/usr/local/lib/python2.7/dist-packages/gdata/blogger/client.py,第111行,add_post
return self.post(new_entry,BLOG_POST_URL%blog_id, auth_token = auth_token,** kwargs)
文件/usr/local/lib/python2.7/dist-packages/gdata/client.py,第690行,post
desired_class = desired_class,* * kwargs)
文件/usr/local/lib/python2.7/dist-packages/gdata/client.py,第298行,请求
** kwargs)
文件 /usr/local/lib/python2.7/dist-packages/gdata/client.py,第307行,请求
响应,未授权)
gdata.client.Unauthorized:未经授权 - 服务器响应:401,用户无权创建新帖子

有人可以帮我吗?我真的很感激:)



问候语

解决方案

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



获得授权令后,我会生成一个授权网址以获取 auth2token.generate_authorize_url(redirect_uri = URL,approval_prompt =force)的访问代码。 code>



一旦您获得此网址,您手动获取代码并生成刷新令牌,您可以通过该令牌生成访问令牌:
token = auth2token.get_access_token(code)。足够容易有关如何将令牌保存到文件中的blob字符串的任何其他信息,请参阅:
gdata-python-api +使用简单验证的分析


Good evening, i've been trying to migrate my blogger python app to oauth2 since the good old Clientlogin() has been deprecated and erased. So, basically i searched through the entire web and couldn't manage to make my application to work correctly.

This is the basic code im using for testing:

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")

I get a 401 error code, unauthorized everytime i try to do this.

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

Can someone help me out with this? I'd really appreciate it :)

Greetings

解决方案

Finally i've fixed my issue with gdata.gauth: I used auth2token = gdata.gauth.OAuth2Token(client_id,client_secret,scope,user_agent)

After getting the authorization token i generate an authorized url to get an access code with auth2token.generate_authorize_url(redirect_uri=URL,approval_prompt="force").

once you get this url, you manually get the code and generate a refresh token, with which you generate an access token: token = auth2token.get_access_token(code). Easy enough. For any other information as to how to save the token to a blob string in a file here's the reference: gdata-python-api + Analytics with simple auth

这篇关于在python中尝试使用gdata和oauth2时遇到麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆