Admin SDK and 2 Legged Oauth

北城余情 提交于 2019-12-04 20:35:24

The new Admin SDK Directory and Reports APIs support OAuth 1.0 authentication including 2-legged OAuth. However, the Google API Python Client library has removed OAuth 1.0 support.

If possible, you should upgrade to OAuth 2.0 and (possibly) Service Accounts (2-legged replacement) for your application.

I did have a Apps Marketplace App that I wanted to use the Directory API with but marketplace is still 2-legged OAuth only. I managed to get the Directory API working with 2-legged using the gdata library to authenticate and low level POST / GET library calls. A quick example:

Get user in Google Apps using 2LO, Admin API and old GData library:

gapps = gdata.client.GDClient()
gapps.ssl = True
gapps.auth_token = gdata.gauth.TwoLeggedOAuthHmacToken(two_legged.key, two_legged.secret, user_email)
uri = 'https://www.googleapis.com/admin/directory/v1/users/%s' % user_email
user_results = gapps.request(method='GET', uri=uri)
user_json = json.loads(user_results.read())

A better way to do this might be to utilize the old GData library to generate the needed 2-legged OAuth headers, then rip those headers out and stick them into discovery objects generated by the new Google API library but I've not yet worked out wha that would look like, shouldn't be terribly hard though.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!