使用python Blogger API时Status参数不起作用 [英] Status parameter not working when using python blogger api

查看:89
本文介绍了使用python Blogger API时Status参数不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将google-api-python-client 1.12.5与Python 3.8下的服务帐户auth一起使用.在我看来,当指定status参数时,Google会以404 HTTP代码进行响应.我不知道为什么.我也查看了文档,但无法将此错误与任何事情联系起来.

I'm trying to use google-api-python-client 1.12.5 with Service account auth under Python 3.8. It seems to me that the when specifying the status parameter, Google responds with a 404 HTTP code. I can't figure out why. I also looked in the docs but I can't relate anything to this error.

我粘贴了我的代码.错误发生在第三个调用中.

I have pasted my code. The error is happening in the third call.

这是代码:

from google.oauth2 import service_account
from googleapiclient.discovery import build

SCOPES = ['https://www.googleapis.com/auth/blogger']
SERVICE_ACCOUNT_FILE = 'new_service_account.json'
BLOG_ID = '<your_blog_id>'

credentials = service_account.Credentials.from_service_account_file(
        SERVICE_ACCOUNT_FILE, scopes=SCOPES)

service = build('blogger', 'v3', credentials=credentials)
p = service.posts()

# FIRST
promise = p.list(blogId=BLOG_ID)
result = promise.execute()

# SECOND
promise = p.list(blogId=BLOG_ID, orderBy='UPDATED')
result = promise.execute()


#THIRD
promise = p.list(blogId=BLOG_ID, orderBy='UPDATED', status='DRAFT')
result = promise.execute()  # <===== ERROR HAPPENS HERE!!!!

service.close()

这是回溯:

Traceback (most recent call last):
  File "/home/madtyn/.local/share/JetBrains/Toolbox/apps/PyCharm-P/ch-0/202.7660.27/plugins/python/helpers/pydev/pydevd.py", line 1448, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/home/madtyn/.local/share/JetBrains/Toolbox/apps/PyCharm-P/ch-0/202.7660.27/plugins/python/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/home/madtyn/PycharmProjects/blogger/main.py", line 24, in <module>
    result = promise.execute()
  File "/home/madtyn/venvs/blogger/lib/python3.8/site-packages/googleapiclient/_helpers.py", line 134, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/home/madtyn/venvs/blogger/lib/python3.8/site-packages/googleapiclient/http.py", line 915, in execute
    raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 404 when requesting https://blogger.googleapis.com/v3/blogs/<blog_id>/posts?orderBy=UPDATED&status=DRAFT&alt=json returned "Not Found">
python-BaseException

推荐答案

我可以重现此问题...添加status = DRAFT将返回404,但其他任何过滤器都有效...

I can reproduce this issue... Adding status=DRAFT will return 404 but any other filter is working...

  1. 尝试使用服务帐户和您的代码:404

  1. Tried with service account and your code: 404

已尝试使用API​​密钥,例如result = requests.get('https://blogger.googleapis.com/v3/blogs/<blog_id>/posts?status=DRAFT&orderBy=UPDATED&alt=json&key=<api_key>'):404

Tried with API Key like this result = requests.get('https://blogger.googleapis.com/v3/blogs/<blog_id>/posts?status=DRAFT&orderBy=UPDATED&alt=json&key=<api_key>'): 404

提取了"access_token"从服务帐户(通话后为credentials.token):result = requests.get('https://blogger.googleapis.com/v3/blogs/<blog_id>/posts?status=DRAFT&orderBy=UPDATED&alt=json&access_token=<extracted_service_account_token>'):404

Extracted "access_token" from service account (credentials.token after a call): result = requests.get('https://blogger.googleapis.com/v3/blogs/<blog_id>/posts?status=DRAFT&orderBy=UPDATED&alt=json&access_token=<extracted_service_account_token>'): 404

但是如果我使用尝试此API"给出的access_token ,很奇怪:是可行的!

But very strangely if I use access_token given by "Try this API" here : https://developers.google.com/blogger/docs/3.0/reference/posts/list?apix_params={"blogId"%3A"blog_id"%2C"orderBy"%3A"UPDATED"%2C"status"%3A["DRAFT"]%2C"alt"%3A"json"} it's works !

在请求中使用该令牌使我的博客帖子处于草稿状态...

Used that token with requests give me my blog post in draft status...

只需在脚本中复制/粘贴原始Authorization标头:

Just copy/paste raw Authorization header inside that script:

import requests

blog_id = '<blog_id>'
headers = {
    'Authorization' : 'Bearer <replace_here>'
}

# Using only Authorization header
result = requests.get(
    'https://blogger.googleapis.com/v3/blogs/%s/posts?status=DRAFT&orderBy=UPDATED&alt=json' % (blog_id),
    headers=headers
)
print(result)
# This should print DRAFT if you have at least one draft post
print(result.json()['items'][0]['status'])

# Using "access_token" param constructed with Authorization header splited to have only token
result = requests.get('https://blogger.googleapis.com/v3/blogs/%s/posts?status=DRAFT&orderBy=UPDATED&alt=json&access_token=%s' % (blog_id, headers['Authorization'][len('Bearer '):]))
print(result)
# This should print DRAFT if you have at least one draft post
print(result.json()['items'][0]['status'])

我目前的结果:

该错误似乎不是来自库,而是来自令牌权限...但是,我通常也使用控制台来生成像您这样的访问.

The bug doesn't seem to come from the library but rather from the token rights...However I also used the console normally to generate accesses like you.

总而言之,我认为这是错误或者是Google自愿提供的...我不知道尝试使用此API"需要多长时间?令牌有效,但这是我找到文章草稿的唯一途径...也许您可以尝试打开错误凭单,但我不知道具体在哪里可以这样做.

To conclude I think it's either a bug or it's voluntary from Google... I don't know how long the "Try this API" token is valid but it is currently the only way I found to get the draft articles... Maybe you can try to open a bug ticket but I don't know specifically where it is possible to do that.

这篇关于使用python Blogger API时Status参数不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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