本文介绍了将电子表格API OAuth2与证书身份验证结合使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将Gdata Spreadsheet API与OAuth2结合使用.

I am trying to use Gdata Spreadsheet API with OAuth2.

使用OAuth2.0 ClientID与域上的OAuth2WebServerFlow配合使用,但是使用服务帐户/证书会导致400 BAD请求

Using OAuth2.0 ClientID works with OAuth2WebServerFlow on a domain,But using Service Accounts / Certificate causes a 400 BAD Request

使用的范围是

https://www.googleapis.com/auth/userinfo.email 
https://www.googleapis.com/auth/userinfo.profile 
https://www.googleapis.com/auth/admin.directory.group.readonly 
https://www.googleapis.com/auth/admin.directory.user.readonly 
https://docs.google.com/feeds/ 
https://spreadsheets.google.com/feeds

这是我正在使用的代码

成功:OAuth2.0

SUCCEEDS : OAuth2.0

flow = OAuth2WebServerFlow(client_id=CLIENT_ID,
                           client_secret=CLIENT_SECRET,
                           scope=SCOPE,
                           access_type = "online",
                           redirect_uri=REDIRECT_URI)
credentials = flow.step2_exchange(code)
auth2token = gauth.OAuth2Token(
                            client_id=credentials.client_id,
                            client_secret=credentials.client_secret,
                            scope=SCOPE,
                            access_token=credentials.access_token,
                            refresh_token=credentials.refresh_token,
                            user_agent='spreadsheetclient/1.0',)
client = SpreadsheetsClient(auth_token=auth2token)
auth2token.authorize(client)
q = SpreadsheetQuery(title= "ItemMaster",title_exact=True)
feed = client.get_spreadsheets(query = q)

失败的Oauth2.0证书

FAILS Oauth2.0 Certificate

credentials = SignedJwtAssertionCredentials(
                        SERVICE_ACCOUNT_EMAIL,
                        CERTIFICATE,
                        scope = SCOPE,
                        prn = "[email protected]"
                        )
http = httplib2.Http()
http = credentials.authorize(http)
auth2token = gauth.OAuth2Token(
                        client_id=credentials.client_id,
                        client_secret=credentials.client_secret,
                        scope=SCOPE,
                        access_token=credentials.access_token,
                        refresh_token=credentials.refresh_token,
                        user_agent='spreadsheetclient/1.0',)
client = SpreadsheetsClient()
auth2token.authorize(client)
q = SpreadsheetQuery(title= "ItemMaster",title_exact=True,)
feed = client.get_spreadsheets(query = q)

那么有没有一种方法可以使证书认证适用于Gdata API?

So is there a way to get Certificate authentication to work for Gdata API ?

推荐答案

我使用OAuth2TokenFromCredentials

    credentials = SignedJwtAssertionCredentials(
                    SERVICE_ACCOUNT_EMAIL,
                    PRIVATE_KEY,
                    scope = SCOPE,
                    sub = "[email protected]")
    auth2token = gauth.OAuth2TokenFromCredentials(credentials)
    client = SpreadsheetsClient()
    auth2token.authorize(client)
    q = SpreadsheetQuery(title= "ItemMaster",title_exact=True,)
    feed = client.get_spreadsheets(query = q)
    self.response.write(feed)

这篇关于将电子表格API OAuth2与证书身份验证结合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 04:39