【问题标题】:Using Spreadsheet API OAuth2 with Certificate Authentication将电子表格 API OAuth2 与证书身份验证一起使用
【发布时间】:2013-12-11 03:06:56
【问题描述】:

我正在尝试将 Gdata 电子表格 API 与 OAuth2 一起使用。

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

使用的范围是

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

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 证书失败

credentials = SignedJwtAssertionCredentials(
                        SERVICE_ACCOUNT_EMAIL,
                        CERTIFICATE,
                        scope = SCOPE,
                        prn = "admin@domain.com"
                        )
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 工作?

【问题讨论】:

    标签: python gdata google-oauth google-spreadsheet-api google-apps-marketplace


    【解决方案1】:

    我用OAuth2TokenFromCredentials解决了这个问题

        credentials = SignedJwtAssertionCredentials(
                        SERVICE_ACCOUNT_EMAIL,
                        PRIVATE_KEY,
                        scope = SCOPE,
                        sub = "admin@domain.com")
        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)
    

    【讨论】:

      猜你喜欢
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-11
      • 1970-01-01
      相关资源
      最近更新 更多