Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Google Blogger API with Python?

I'm trying to use the blog functions from Google API gdata. I tried my best following the documentation but I failed very badly. Can anybody tell me how can I use the Google blogger API? My code is pretty messed up and now I'm out of clue.

EDIT FULL WORKING CODE :) :

from oauth2client.client import OAuth2WebServerFlow
import httplib2
from apiclient.discovery import build
from oauth2client.file import Storage

#flow = OAuth2WebServerFlow(client_id='', #ID
#                           client_secret='', #SECRET ID
#                           scope='https://www.googleapis.com/auth/blogger',
#                           redirect_uri='urn:ietf:wg:oauth:2.0:oob')

#auth_uri = flow.step1_get_authorize_url()
# Redirect the user to auth_uri on your platform.

# Open a file
#fo = open("foo.txt", "wb")
#fo.write( auth_uri +"\n");
#fo.close()

#credentials = flow.step2_exchange( raw_input ( ) ) 


storage = Storage('a_credentials_file')
#storage.put(credentials)

credentials = storage.get()

http = httplib2.Http()
http = credentials.authorize(http)

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

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'])
like image 627
ryspbsk Avatar asked Jun 16 '16 06:06

ryspbsk


People also ask

Can I use Python in Blogger?

You can use the Python client library to publish new blog entries. First, create a GDataEntry instance to represent the blog post.

How do I integrate API on Blogger?

Requests to the Blogger APIs for public data must be accompanied by an identifier, which can be an API key or an access token. Or create one in the Credentials page. After you have an API key, your application can append the query parameter key= yourAPIKey to all request URLs.

What is Blogger API?

The Blogger API v3 allows client applications to view and update Blogger content. Your client application can use Blogger API v3 to create new blog posts, edit or delete existing posts, and query for posts that match particular criteria.

How do I recover my blogspot account?

Go to the "Forgot your username or password?" page. Enter the URL of your blog. You'll get a hint about which email address is connected with your blog. You'll also get an email with instructions to sign in.


1 Answers

While I was myself trying to find a solution, I found this. Then after some modifications, the code finally worked. It successfully print all details about you blog site.

from oauth2client.client import flow_from_clientsecrets
import httplib2
from apiclient.discovery import build
from oauth2client.file import Storage
import webbrowser

def get_credentials():
    scope = 'https://www.googleapis.com/auth/blogger'
    flow = flow_from_clientsecrets(
        'client_secrets.json', scope,
        redirect_uri='urn:ietf:wg:oauth:2.0:oob')
    storage = Storage('credentials.dat')
    credentials = storage.get()

    if  not credentials or credentials.invalid:
        auth_uri = flow.step1_get_authorize_url()
        webbrowser.open(auth_uri)
        auth_code = raw_input('Enter the auth code: ')
        credentials = flow.step2_exchange(auth_code)
        storage.put(credentials)
    return credentials

def get_service():
    """Returns an authorised blogger api service."""
    credentials = get_credentials()
    http = httplib2.Http()
    http = credentials.authorize(http)
    service = build('blogger', 'v3', http=http)
    return service

if __name__ == '__main__':
    served = get_service()
    blogs = served.blogs()
    blog_get_obj = blogs.get(blogId='123456789123456')
    details = blog_get_obj.execute()
    print details

The results of print will look like:

{u'description': u'Look far and wide. There are worlds to conquer.',
 u'id': u'8087466742945672359',
 u'kind': u'blogger#blog',
 u'locale': {u'country': u'', u'language': u'en', u'variant': u''},
 u'name': u'The World Around us',
 u'pages': {u'selfLink': u'https://www.googleapis.com/blogger/v3/blogs/1234567897894569/pages',
            u'totalItems': 2},
 u'posts': {u'selfLink': u'https://www.googleapis.com/blogger/v3/blogs/1245678992359/posts',
            u'totalItems': 26},
 u'published': u'2015-11-02T18:47:02+05:30',
 u'selfLink': u'https://www.googleapis.com/blogger/v3/blogs/9874652945672359',
 u'updated': u'2017-06-29T19:41:00+05:30',
 u'url': u'http://www.safarnuma.com/'}
like image 131
Arindam Roychowdhury Avatar answered Oct 07 '22 21:10

Arindam Roychowdhury