I have an "Authorization Code" for my application. But how can I use it to publish to a blogger using gdata-python-client?

I am using gdata-pyton-client. I have an "Authorization Code" for my application. But what now? How can I use it to post to a blogger?

I used the following code and got an authorization code,

CLIENT_ID = 'my-client-id' CLIENT_SECRET = 'my-secret' SCOPES = ['https://www.googleapis.com/auth/blogger'] USER_AGENT = 'my-app' token = gdata.gauth.OAuth2Token( client_id=CLIENT_ID, client_secret=CLIENT_SECRET, scope=' '.join(SCOPES), user_agent=USER_AGENT) print token.generate_authorize_url(redirect_url='urn:ietf:wg:oauth:2.0:oob') print token.get_access_token(TOKEN-THAT-I-GOT-FROM-ABOVE-URL) 

But how to use it?

How can I authorize a blogger to post to a blogger? I used this example for my testing purpose: https://code.google.com/p/gdata-python-client/source/browse/samples/blogger/BloggerExampleV1.py

But for this, email and password are used. How can I use an access token?

+4
source share
2 answers

you can try below.

Please make sure all imported products are below.

 from apiclient.discovery import build from oauth2client.file import Storage from oauth2client.client import AccessTokenRefreshError from oauth2client.client import flow_from_clientsecrets from oauth2client.tools import run 

Set up your client_secrets.JSON like this

setting client_secrets.json as

 { "web": { "client_id": "[[INSERT CLIENT ID HERE]]", "client_secret": "[[INSERT CLIENT SECRET HERE]]", "redirect_uris": [], "auth_uri": "https://accounts.google.com/o/oauth2/auth", "token_uri": "https://accounts.google.com/o/oauth2/token" } } 

For future reference, you can save your credentials in the blogger.dat file for faster processing.

 FLOW = flow_from_clientsecrets(Path_to_client_secrets.json,scope='https://www.googleapis.com/auth/blogger',message=MISSING_CLIENT_SECRETS_MESSAGE) storage = Storage('blogger.dat') credentials = storage.get() if credentials is None or credentials.invalid: credentials = run(FLOW, storage) 

After setting credentials. Its time for publication! therefore, we create an httplib2.Http object to process our HTTP requests and authorize with our good credentials.

 http = httplib2.Http() http = credentials.authorize(http) service = build("blogger", "v2", http=http) 

After creating, we will create the body of the blog and publish

 try: body = { "kind": "blogger#post", "id": "6814573853229626501", "title": "posted via python", "content":"<div>hello world test</div>" } request = service.posts().insert(your_blogId_ID,body) response = request.execute() print response except AccessTokenRefreshError: print ("The credentials have been revoked or expired, please re-run the application to re-authorize") 

hope this helps.

+1
source

See this documentation page for instructions on how to use markers, in particular the example at the end:

 # Find a token to set the Authorization header as the request is being made token = self.token_store.find_token(url) # Tell the token to perform the request using the http_client object # By default, the http_client is an instance of atom.http.HttpClient which uses httplib to make requests token.perform_request(self.http_client, 'GET', url, data=None, headers) 
+1
source

All Articles