谷歌OAuth令牌请求返回“invalid_client”:“未经授权”

14

尝试让OAuth2 Google登录工作,这是我的应用程序发出的原始请求:

方法:POST

URL:https://www.googleapis.com/oauth2/v3/token

标头:Content-Type: application/x-www-form-urlencoded

值:

client_idXXX-0123456789abcdef0123456789abcdef.apps.googleusercontent.com

client_secretA1b2C3d4E5f6G7h8I9j0K1l2M

code1/A1b2C3d4E5f6G7h8I9j0K1l2M3n4O5p6Q7r8S9t0U1v

grant_typeauthorization_code

redirect_urihttp://localhost:5000/callback/google/

这是响应:

状态:401 Unauthorized

正文:

{
    "error": "invalid_client",
    "error_description": "Unauthorized"
}

我已经验证过这是我的应用程序发出的确切请求/响应(使用Flask和rauth的Python应用程序),并且已经验证我可以使用Postman复制完全相同的请求/响应。

根据其他主题中的说明,在Google API控制台中执行了以下所有操作:

  • 在“OAuth同意屏幕”设置中,将“产品名称”设置为与“项目名称”不同的名称
  • 还在“OAuth同意屏幕”设置中,双重检查电子邮件是否设置
  • 启用Google+ API
  • 启用Gmail API
  • 重新创建客户端ID /密钥
  • 仔细检查客户端ID /密钥值中是否有前导或尾随空格,我已正确从API控制台复制它们

无论我做什么,仍然得到相同的"invalid_client": "Unauthorized"响应。

对此的帮助将不胜感激。我正在尝试在我的应用程序中设置基于OAuth2的“使用X登录”功能,并已成功处理Facebook和Twitter,希望也能使Google工作,但如果我无法解决此问题,我恐怕必须放弃Google认证。

2个回答

12

无效的客户端表示您使用的客户端ID或客户端密钥不正确。它们必须是您从Google开发者控制台下载的。

提示:您可能想考虑使用Google Python客户端库,它可以为您完成所有繁重的工作。

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/drive.metadata.readonly']

def main():
    """Shows basic usage of the Drive v3 API.
    Prints the names and ids of the first 10 files the user has access to.
    """
    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(
                'credentials.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('drive', 'v3', credentials=creds)

    # Call the Drive v3 API
    results = service.files().list(
        pageSize=10, fields="nextPageToken, files(id, name)").execute()
    items = results.get('files', [])

    if not items:
        print('No files found.')
    else:
        print('Files:')
        for item in items:
            print(u'{0} ({1})'.format(item['name'], item['id']))

if __name__ == '__main__':
    main()

2
我不小心在我的秘密字符串中添加了一个空格,这导致了问题。 - sameerfair
3
花了一整天去做这个,差点就彻底崩溃了。最终把查询字符串打印出来才发现我忘记传递client_secret变量,所以它的值是未定义的;-;感觉自己太蠢了。 - Nayeemul Islam Swad
1
我刷新了客户端密钥文件,但仍然收到“invalid_client/unauthorized”错误。我不得不删除token.pickle。一旦使用新的客户端密钥重新创建token.pickle,错误就消失了。 - stroopwafel
1
@NayeemulIslamSwad 我看了你的评论并检查了自己是否犯了同样的错误,没错我也忘记传递秘密 ID 了。 - Arpit
1
@sameerfair 我也是,四年后在一个 Next-Auth 应用中!世界不断变化,但程序员仍然是程序员 :D - Paulo Brito
显示剩余5条评论

1

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