【问题标题】:Keep running into errors with the Google Cloud API不断遇到 Google Cloud API 错误
【发布时间】:2021-06-30 14:20:04
【问题描述】:

最近我一直在使用 Google Cloud API 进行一些数据分析项目。目前,我一直遵循其他网站的说明,并以此为基础制作自己的数据集。但是,我一直在使用 Google Cloud API 时遇到错误。

例如,我正在尝试以下文章中的方法:https://practicaldatascience.co.uk/data-science/how-to-query-the-google-search-console-api-with-ecommercetools

然而,Python 的响应如下:

错误:服务帐户信息不是预期的格式,缺少字段 token_uri、client_email。

我在文章中提到的“OAuth 2.0 客户端 ID”下创建了基本凭据。

这是我目前拥有的代码:

import pandas as pd
from ecommercetools import seo
from oauth2client.service_account import ServiceAccountCredentials

pd.set_option('max_colwidth', 100)

key = "G:\Python Projects\query_gsc_secrets.json"
site_url = "https://startselect.com/"
start_date = "2019-01-01"
end_date = "2020-12-31"

print(key)


payload = {
    'startDate': start_date,
    'endDate': end_date,
    'dimensions': ["page"],
    'rowLimit': 10000,
    'startRow': 0
}

df = seo.query_google_search_console(key, site_url, payload)
df.sort_values(by='clicks', ascending=False).head()

我问这个的原因是因为这个错误似乎以一种或另一种形式重复出现。创建凭据时我是否遗漏了什么?代码有错误吗?

提前致谢!

【问题讨论】:

    标签: python api google-cloud-platform google-cloud-functions


    【解决方案1】:

    密钥的格式似乎与预期不符。

    您提到您在“OAuth 2.0 客户端 ID”下创建了基本凭据,但如果您通读 the linked article,您会看到 API 需要一个服务帐户密钥。

    服务帐户密钥格式如下所示:

    {
    "type": "service_account",
    "project_id": "project-id",
    "private_key_id": "key-id",
    "private_key": "-----BEGIN PRIVATE KEY-----\nprivate-key\n-----END PRIVATE KEY-----\n",
    "client_email": "service-account-email",
    "client_id": "client-id",
    "auth_uri": "https://accounts.google.com/o/oauth2/auth",
    "token_uri": "https://accounts.google.com/o/oauth2/token",
    "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
    "client_x509_cert_url": 
    "https://www.googleapis.com/robot/v1/metadata/x509/service-account-email"
    }
    

    与错误中请求的格式匹配 missing fields token_uri, client_email.

    在此 GCP 文档中,您可以阅读如何使用 create a service accountservice account key

    【讨论】:

      猜你喜欢
      • 2021-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-14
      • 2021-08-19
      相关资源
      最近更新 更多