我在尝试获取用于获取刷新令牌的认证代码时遇到了访问被阻止的错误。

huangapple go评论103阅读模式
英文:

I am getting Access blocked error while trying to get athentication code which i will use to get refresh token?

问题

这是我尝试的内容:
我将OAuth Consent Screen的状态从测试更改为发布,并且应用范围是external,然后我创建了OAuth客户端ID令牌,然后我尝试了这段代码,但当我尝试对应用进行身份验证时,它会出现错误。

  1. from google.oauth2.credentials import Credentials
  2. from google_auth_oauthlib.flow import InstalledAppFlow
  3. from google.auth.transport.requests import Request
  4. SCOPES = ['https://www.googleapis.com/auth/drive']
  5. CLIENT_SECRETS_FILE = 'client.json'
  6. REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob'
  7. flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRETS_FILE, SCOPES, redirect_uri=REDIRECT_URI)
  8. auth_url, _ = flow.authorization_url(prompt='consent')
  9. print(f'请访问此URL以授权应用程序:{auth_url}')
  10. auth_code = input('输入授权码:')
  11. flow.fetch_token(code=auth_code)
  12. creds = flow.credentials
  13. print(f'访问令牌:{creds.token}')
  14. print(f'刷新令牌:{creds.refresh_token}')

你能看出如何在Python中实现它并解决此错误吗?

英文:

here is what is tried
I changed the status of O Auth Consent screen from testing to publish and the app scope is external then i created the O Auth client Id token and then i tried this code but this is giving error when i try to authenticate to the app.

  1. from google.oauth2.credentials import Credentials
  2. from google_auth_oauthlib.flow import InstalledAppFlow
  3. from google.auth.transport.requests import Request
  4. SCOPES = ['https://www.googleapis.com/auth/drive']
  5. CLIENT_SECRETS_FILE = 'client.json'
  6. REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob'
  7. flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRETS_FILE, SCOPES, redirect_uri=REDIRECT_URI)
  8. auth_url, _ = flow.authorization_url(prompt='consent')
  9. print(f'Please go to this URL to authorize the application: {auth_url}')
  10. auth_code = input('Enter the authorization code: ')
  11. flow.fetch_token(code=auth_code)
  12. creds = flow.credentials
  13. print(f'Access token: {creds.token}')
  14. print(f'Refresh token: {creds.refresh_token}')

Can you spot how to do it in python and solve this error.

答案1

得分: 1

你不能使用 urn:ietf:wg:oauth:2.0:oob,因为它已经停用。

你最好按照官方的快速入门来操作。

此示例将使用 creds = flow.run_local_server(port=0) 来打开授权屏幕,而不是要求你点击链接,因为这样可能会返回 404 错误,因为你没有本地 Web 服务器在运行。

  1. from __future__ import print_function
  2. import os.path
  3. from google.auth.transport.requests import Request
  4. from google.oauth2.credentials import Credentials
  5. from google_auth_oauthlib.flow import InstalledAppFlow
  6. from googleapiclient.discovery import build
  7. from googleapiclient.errors import HttpError
  8. # 如果要修改这些范围,请删除 token.json 文件。
  9. SCOPES = ['https://www.googleapis.com/auth/drive.metadata.readonly']
  10. def main():
  11. """展示了 Drive v3 API 的基本用法。
  12. 打印用户可以访问的前 10 个文件的名称和 ID。
  13. """
  14. creds = None
  15. # 文件 token.json 存储了用户的访问令牌和刷新令牌,在第一次授权流完成后会自动生成。
  16. if os.path.exists('token.json'):
  17. creds = Credentials.from_authorized_user_file('token.json', SCOPES)
  18. # 如果没有(有效的)凭据可用,让用户登录。
  19. if not creds or not creds.valid:
  20. if creds and creds.expired and creds.refresh_token:
  21. creds.refresh(Request())
  22. else:
  23. flow = InstalledAppFlow.from_client_secrets_file(
  24. 'credentials.json', SCOPES)
  25. creds = flow.run_local_server(port=0)
  26. # 保存凭据以备下次运行使用
  27. with open('token.json', 'w') as token:
  28. token.write(creds.to_json())
  29. try:
  30. service = build('drive', 'v3', credentials=creds)
  31. # 调用 Drive v3 API
  32. results = service.files().list(
  33. pageSize=10, fields="nextPageToken, files(id, name)").execute()
  34. items = results.get('files', [])
  35. if not items:
  36. print('未找到文件。')
  37. return
  38. print('文件:')
  39. for item in items:
  40. print(u'{0} ({1})'.format(item['name'], item['id']))
  41. except HttpError as error:
  42. # TODO(开发者)- 处理来自 Drive API 的错误。
  43. print(f'发生错误:{error}')
  44. if __name__ == '__main__':
  45. main()
英文:

you cant use urn:ietf:wg:oauth:2.0:oob, this was discontinued.

You would have better luck following the official QuickStart

This sample will use creds = flow.run_local_server(port=0) to open the consent screen for you rather then asking you to click a link which will probably return a 404 error because you don't have a local web server running.

  1. from __future__ import print_function
  2. import os.path
  3. from google.auth.transport.requests import Request
  4. from google.oauth2.credentials import Credentials
  5. from google_auth_oauthlib.flow import InstalledAppFlow
  6. from googleapiclient.discovery import build
  7. from googleapiclient.errors import HttpError
  8. # If modifying these scopes, delete the file token.json.
  9. SCOPES = ['https://www.googleapis.com/auth/drive.metadata.readonly']
  10. def main():
  11. """Shows basic usage of the Drive v3 API.
  12. Prints the names and ids of the first 10 files the user has access to.
  13. """
  14. creds = None
  15. # The file token.json stores the user's access and refresh tokens, and is
  16. # created automatically when the authorization flow completes for the first
  17. # time.
  18. if os.path.exists('token.json'):
  19. creds = Credentials.from_authorized_user_file('token.json', SCOPES)
  20. # If there are no (valid) credentials available, let the user log in.
  21. if not creds or not creds.valid:
  22. if creds and creds.expired and creds.refresh_token:
  23. creds.refresh(Request())
  24. else:
  25. flow = InstalledAppFlow.from_client_secrets_file(
  26. 'credentials.json', SCOPES)
  27. creds = flow.run_local_server(port=0)
  28. # Save the credentials for the next run
  29. with open('token.json', 'w') as token:
  30. token.write(creds.to_json())
  31. try:
  32. service = build('drive', 'v3', credentials=creds)
  33. # Call the Drive v3 API
  34. results = service.files().list(
  35. pageSize=10, fields="nextPageToken, files(id, name)").execute()
  36. items = results.get('files', [])
  37. if not items:
  38. print('No files found.')
  39. return
  40. print('Files:')
  41. for item in items:
  42. print(u'{0} ({1})'.format(item['name'], item['id']))
  43. except HttpError as error:
  44. # TODO(developer) - Handle errors from drive API.
  45. print(f'An error occurred: {error}')
  46. if __name__ == '__main__':
  47. main()

huangapple
  • 本文由 发表于 2023年3月1日 16:09:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75600994.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定