gpt4 book ai didi

python - 插入帖子 Blogger API 使用 Python 失败

转载 作者:行者123 更新时间:2023-11-28 22:01:14 24 4
gpt4 key购买 nike

此代码现在有效。

我在通过 python2.7 调用 API 将新博客文章插入到 google 的 Blogger 站点时遇到问题。我拥有来自谷歌的所有 oauth2client 模块来处理身份验证。我有权使用 Blogger V3 api - 这是在谷歌开发者控制台上激活的。我已经使用有效的相同 credentials.dat 运行了简单的 api 请求:

这有效(不包括完整代码)

service = build('blogger','v3', http=http)
try:
request = service.blogs().get(blogId="6814573853229626501")
response = request.execute()
print response

google api 发现服务让我相信这是插入帖子的代码的样子 https://developers.google.com/apis-explorer/#p/blogger/v3/blogger.posts.insert

service = build('blogger','v3', http=http)

try:
body = {
"kind": "blogger#post",
"id": "6814573853229626501",
"title": "posted via python",
"content":"<div>hello world test</div>"
}

request = service.posts().insert(blogId="6814573853229626501",body=body)

response = request.execute()
print response

我确定是我搞砸了 body=body 部分?有什么线索吗?

这是我得到的错误:

Traceback (most recent call last):
File "blogger.py", line 104, in <module>
main()
File "blogger.py", line 93, in main
response = request.execute()
File "/usr/local/lib/python2.7/dist-packages/google_api_python_client-1.0c2-py2.7.egg/apiclient/http.py", line 654, in execute
raise HttpError(resp, content, self.uri)
apiclient.errors.HttpError: <HttpError 400 when requesting https://www.googleapis.com/blogger/v3/blogs/6814573853229626501/posts?alt=json returned "Invalid Value">

如果您有兴趣,我正在尝试从我当时感兴趣的 eBay 数据生成的 google 融合表中发布图表。

最佳答案

您可以通过某种方式在任何博客上发帖

__author__ = 'spandey2405@gmail.com (Saurabh Pandey)'

import sys
from oauth2client import client
from googleapiclient import sample_tools

# Authenticate and construct service.
service, flags = sample_tools.init(
argv, 'blogger', 'v3', __doc__, __file__,
scope='https://www.googleapis.com/auth/blogger')

try:

users = service.users()

# Retrieve this user's profile information
thisuser = users.get(userId='self').execute()
print('This user\'s display name is: %s' % thisuser['displayName'])

blogs = service.blogs()

# Retrieve the list of Blogs this user has write privileges on
thisusersblogs = blogs.listByUser(userId='self').execute()
for blog in thisusersblogs['items']:
print('The blog named \'%s\' is at: %s' % (blog['name'], blog['url']))

posts = service.posts()
body = {
"kind": "blogger#post",
"id": "6701167141462934671",
"title": "posted via python",
"content":"<div>hello world test</div>"
}
insert = posts.insert(blogId='6701167141462934671', body=body)
posts_doc = insert.execute()
print posts_doc


except client.AccessTokenRefreshError:
print ('The credentials have been revoked or expired, please re-run'
'the application to re-authorize')

关于python - 插入帖子 Blogger API 使用 Python 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13317113/

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