skip to Main Content

This code now works.

I’m having an issue inserting a new blog post on to google’s Blogger site via python2.7 calling an API.
I have all the oauth2client modules from google to handle the authentication.
I have permission to use the Blogger V3 api – this is activated on the google developer console.
I have run simple api requests with the same credentials.dat that have worked:

this worked (full code not included)

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

The google api discovery service leads me to believe this is what the code should look like to insert a post
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

I’m sure it’s the body=body part that I’m messing up? Any clues?

here is the error that I get:

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">

If you’re interested I’m experimenting with posting charts from my google fusion tables generated by eBay data that i’m interested in at the time.

2

Answers


  1. You do not need the “data”: object wrapper around your data, the client library will add that if the server needs it. This documentation shows the form of the object to use in the insert call:

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

    Login or Signup to reply.
  2. You can post on any blogger somehow

    __author__ = '[email protected] (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')
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search