使用Python插入Blogger API失败

时间:2012-11-09 22:43:43

标签: python google-api blogger google-api-python-client

此代码现在有效。

我在通过调用API的python2.7向Google的Blogger网站上插入新博客文章时遇到问题。 我有来自谷歌的所有oauth2client模块来处理身份验证。 我有权使用Blogger V3 API - 这是在google开发者控制台上激活的。 我使用相同的凭证运行简单的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数据生成的谷歌融合表中的图表。

2 个答案:

答案 0 :(得分: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')

答案 1 :(得分:1)

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

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