【问题标题】:Insert post Blogger API failing with Python插入帖子 Blogger API 使用 Python 失败
【发布时间】:2012-11-09 22:43:43
【问题描述】:

此代码现在可以工作了。

我在通过调用 API 的 python2.7 将新博客文章插入到谷歌的 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 融合表中发布图表。

【问题讨论】:

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


    【解决方案1】:

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

    __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')
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 太棒了!工作得很好。编辑了上面的代码以显示正确的解决方案。
      • 能否请您分享整个代码,包括验证码?我也有这个问题。提前致谢
      猜你喜欢
      • 1970-01-01
      • 2011-08-05
      • 2017-07-25
      • 1970-01-01
      • 1970-01-01
      • 2014-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多