gpt4 book ai didi

python - 使用python blogger api时状态参数不起作用

转载 作者:行者123 更新时间:2023-12-04 08:41:48 25 4
gpt4 key购买 nike

我正在尝试在 Python 3.8 下使用带有服务帐户身份验证的 google-api-python-client 1.12.5。在我看来,指定状态参数时,Google 会以 404 HTTP 代码响应。我不明白为什么。我也查看了文档,但我无法将任何与此错误相关联。
我已经粘贴了我的代码。错误发生在第三次调用中。
这是代码:

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 但任何其他过滤器都在工作...

  • 尝试使用服务帐户和您的代码:404
  • 尝试使用这样的 API key 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

  • 但很奇怪如果我 使用“尝试此 API”提供的 access_token 这里: 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"}其作品 !
    使用该 token 与请求给我我的博客文章处于草稿状态......
    try this api + network panel
    只需复制/粘贴原始 Authorization该脚本中的标题:
    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'])
    我目前的结果:
    enter image description here
    该错误似乎不是来自库,而是来自 token 权限......但是我通常也使用控制台来生成像您一样的访问。
    总而言之,我认为这要么是一个错误,要么是 Google 自愿提供的……我不知道“试用此 API” token 的有效期有多长,但它目前是我找到的获取文章草稿的唯一方法……也许您可以尝试打开一个错误票,但我不知 Prop 体在哪里可以做到这一点。

    关于python - 使用python blogger api时状态参数不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64527443/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com