尽管使用的是谷歌账户所有者,但从谷歌认证 Web API 收到“Error 403: access_denied”错误信息。

53
我正在使用谷歌提供的默认代码(在此处),但我不太明白为什么它无法正常工作。代码输出提示Please visit this URL to authorize this application: [google login URL]。当尝试使用在谷歌开发者控制台下指定为脚本所有者的帐户登录时,我会收到一个Error 403: access_denied错误,并且还有一条消息The developer hasn’t given you access to this app. It’s currently being tested and it hasn’t been verified by Google. If you think you should have access, contact the developer [the email I just tried to log in with].。请注意保留HTML标记,但不需要进行解释。
from __future__ import print_function
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request

# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly']

# The ID and range of a sample spreadsheet.
SAMPLE_SPREADSHEET_ID = '1vrZpCGW58qCCEfVXoJYlwlulraIlfWI2SmFXa1iPtuU'
SAMPLE_RANGE_NAME = 'Class Data!A2:E'

def main():
    """Shows basic usage of the Sheets API.
    Prints values from a sample spreadsheet.
    """
    creds = None
    # The file token.pickle stores the user's access and refresh tokens, and is
    # created automatically when the authorization flow completes for the first
    # time.
    if os.path.exists('token.pickle'):
        with open('token.pickle', 'rb') as token:
            creds = pickle.load(token)
    # If there are no (valid) credentials available, let the user log in.
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            creds.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(
                'Google_API_Key.json', SCOPES)
            creds = flow.run_local_server(port=0)
        # Save the credentials for the next run
        with open('token.pickle', 'wb') as token:
            pickle.dump(creds, token)

    service = build('sheets', 'v4', credentials=creds)

    # Call the Sheets API
    sheet = service.spreadsheets()
    result = sheet.values().get(spreadsheetId=SAMPLE_SPREADSHEET_ID,
                                range=SAMPLE_RANGE_NAME).execute()
    values = result.get('values', [])

    if not values:
        print('No data found.')
    else:
        print('Name, Major:')
        for row in values:
            # Print columns A and E, which correspond to indices 0 and 4.
            print('%s, %s' % (row[0], row[4]))

def PrintToSheets():
    main()
4个回答

182
为了解决这个问题,我需要做以下简单步骤:
  1. 访问https://console.developers.google.com/
  2. 在左上角的“Google APIs”旁边,点击右侧的项目下拉菜单
  3. 确保已选择正确的项目
  4. 点击屏幕左侧的“OAuth同意屏幕”(在“凭据”下面)
  5. 如果您尚未创建同意屏幕,请先创建
  6. 在“测试用户”下方有一个名为“+ADD USERS”的按钮
  7. 输入您将进行测试的帐户的电子邮件,按Enter,然后点击保存。
  8. 现在应该可以工作了!
看起来他们最近更新了此功能,因为去年我不需要这样做。

5
啊哈!我太匆忙地通过了它,没有创建任何测试用户。谢谢! - deweydb
12
这应该是被接受的答案!非常清晰的说明 :-) - koder613
2
如果您遇到错误 400,请尝试在隐身模式下打开,或尝试邀请具有 IAM 角色的用户。 - Mohammad Zaid Pathan
2
祝福你,Kayden!我希望所有的答案都像这样直截了当! - Ryan Loggerythm
1
上帝保佑你,兄弟! - matrixb0ss
显示剩余3条评论

26
你得到的错误信息与你的应用程序尚未验证有关。如链接中所述,所有使用敏感范围访问Google API的应用程序都需要通过Google的验证过程。通常情况下,在100个用户访问您的应用程序之前,您会获得宽限期,之后您的应用程序将被锁定,并且您将无法再授权更多用户,除非您验证了您的应用程序,听起来您可能已经达到了这一点。
您唯一的选择是通过验证过程或在Google开发人员控制台上创建一个全新的项目,并使用该客户端ID,因为您当前使用的客户端ID已经锁定了额外的用户。
更新更改
Google Developer控制台已实施更改。现在,您必须在应用程序经过验证之前授权用户/测试人员访问您的应用程序。如果您转到项目的Google开发人员控制台,在同意屏幕下,您将找到以下部分。

enter image description here

你可以在这里添加测试用户,但是你不能删除它们,而且你只能添加100个,所以要明智地使用它。


5
谢谢提醒,我之前没有配置我的Oauth2同意屏幕,还以为作为所有者我自动可以访问并运行应用程序。他们应该在谷歌设置应用程序的教程中更清楚地说明这一点:/ - khan simeoni
这是一个相对较新的功能,我猜直到2020年上半年之前都不存在。 - Singh

0

Plesk中

Web应用防火墙模式:设置为"关闭"


-2

针对这个问题,只需从谷歌验证谷歌应用程序帐户即可,它可以正常工作。 输入图像描述


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接