3

这段代码现在可以工作了。

我在通过 python2.7 调用 API 将新博客文章插入到谷歌的 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

谷歌 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

我确定这是我搞砸的身体=身体部分?有什么线索吗?

这是我得到的错误:

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 融合表中发布图表。

4

2 回答 2

4

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

__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')
于 2015-10-28T07:57:42.150 回答
1

您不需要“数据”:围绕数据的对象包装器,如果服务器需要,客户端库将添加它。本文档显示了在插入调用中使用的对象的形式:

https://google-api-client-libraries.appspot.com/documentation/blogger/v3/python/latest/blogger_v3.posts.html#insert

于 2012-11-22T03:29:47.680 回答