简体   繁体   中英

Google service account Delegation

I am working on creating a simple app in google appengine to automatically post contents on my blogspot.

I have created service account and trying to use below code for calling blogger api for posting a blog. This one fails to run with insufficient privileges error message.

urlfetch.set_default_fetch_deadline(45)
service = build('blogger', 'v3')

class MainHandler(webapp2.RequestHandler):
    def post(self):
            body = {
            "kind": "blogger#post",
            "id": "9999999999999999",
            "title": "SAmple title",
            "content": "SAmple blog contents"
            }
            scopes = ['https://www.googleapis.com/auth/blogger']
            credentials = ServiceAccountCredentials.from_json_keyfile_name('secret-key.json', scopes=scopes)
            http_auth     = credentials.authorize(Http())
            request = service.posts().insert(blogId="9999999999999999",body=body)
            response = request.execute(http=http_auth)
            self.response.out.write(pprint.pformat(response))

app = webapp2.WSGIApplication([
    ('/postcontents', MainHandler)
], debug=True)

However i can access the blog details by below line

self.response.write(blogs.get(blogId='9999999999999999').execute(http=http_auth))

It seems like my service account is not associated with my blogger account(though both my google app engine and blogger uses same gmail account). how do i achieve this ?

Blogger doesn't support service accounts. You will need to use Oauth2. Authenticate your code once store the refresh token have your code then use the refresh token to get a new access token as needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM