I am using gdata-pyton-client. I have got the "Authorization code" for my app. But now what? How can I use it to post in the blogger?
I used the following code and get eh 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 now how do I use it?
How can I authorize the blogger, for posting it to the blogger? I have been using this example for my testing purpose: https://code.google.com/p/gdata-python-client/source/browse/samples/blogger/BloggerExampleV1.py
But this is using email & password for login. How can I use the access token?
you can try below solution.
Please ensure that all below imports are there.
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
Setup your client_secrets.JSON like this
setup client_secrets.json like
{
"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 store the Credentials in a file blogger.dat
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)
Once the Credentials are all setup. its time to post! so we Create an httplib2.Http object to handle our HTTP requests and authorize it with our good Credentials.
http = httplib2.Http()
http = credentials.authorize(http)
service = build("blogger", "v2", http=http)
Once done we build the blog body and post
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.