Skip to main content
 首页 » 编程设计

python之使用 OAuth2WebServerFlow 的多个范围

2025年05月04日33cloudgamer

我想使用 OAuth2WebServerFlow() 授权多个范围。这是我当前的代码:

flow = OAuth2WebServerFlow( 
    client_id=id, 
    client_secret=secret, 
    scope= ['scope1','scope2','scope3'], 
    approval_prompt='force', 
    access_type='offline') 

当我被重定向到 Google 授权屏幕时,应用程序要求的唯一权限是“具有离线访问权限”。此外,我在 API 控制台中打开了这些范围。

请您参考如下方法:

来自documentation :

In addition to these OpenID-specific scopes, your scope argument can also include other scope strings. All scope strings must be space-separated. For example, if you wanted per-file access to a user’s Google Drive, your scope might be openid profile email https://www.googleapis.com/auth/drive.file.

所以在你的情况下:

scope= 'scope1 ' + 'scope2 ' + 'scope3' 

示例:

scope = ('https://www.googleapis.com/auth/devstorage.read_write ' + 
         'https://www.googleapis.com/auth/prediction') 

编辑

正如@Greg 提到的:

OAuth2WebServerFlow takes either a string or an iterable of strings, which it turns into the space-separated parameter before constructing the auth-URL.

我倾向于将 oAuth 用于 api(应用程序授权),而不是将 oAuth 用于用户 oauth,所以我忘记了我在自己的应用程序中也观察到了这一点。

如果您的范围有无效 授权,那么您将收到有关错误授权的错误消息。

不过,如果您要求正确 资助,大多数时候它们是结合在一起的。

在下面的示例中,我询问的是 'https://www.googleapis.com/auth/devstorage.read_write ' + 'https://www.googleapis.com/auth/prediction'

但尽管我只收到离线访问提示。 我的应用程序运行良好,权限正确。 该应用程序没有更多权限(其他 api)。