Skip to main content
 首页 » 编程设计

android之如何在 Python 中将 Google 帐户身份验证添加到 Google Cloud Endpoints

2025年05月04日9xing901022

我正在尝试为我的 Android 应用创建一个 Google Cloud Endpoints 后端,它使用用户的 Google 帐户进行身份验证。我发现 Java API 包含针对 OAuth 范围的注释,但 python 文档仅对其进行了一次引用。

这是我在端点上使用的注释:

@endpoints.api(name='notes', version='v1', 
           description='Notes API', 
           allowed_client_ids=[CLIENT_ID, endpoints.API_EXPLORER_CLIENT_ID], 
           audiences=[AUDIENCE], 
           scopes=['https://www.googleapis.com/auth/userinfo.email']) 

但是生成的代码没有任何作用域:

/** 
 * Available OAuth 2.0 scopes for use with the . 
 * 
 * @since 1.4 
 */ 
public class NotesScopes { 
 
  private NotesScopes() { 
  } 
} 

甚至生成的服务类似乎也缺少部分:

/** 
 * The default encoded root URL of the service. This is determined when the library is generated 
 * and normally should not be changed. 
 * 
 * @since 1.7 
 */ 
public static final String DEFAULT_ROOT_URL = "https://None/_ah/api/"; 
 
/** 
 * The default encoded service path of the service. This is determined when the library is 
 * generated and normally should not be changed. 
 * 
 * @since 1.7 
 */ 
public static final String DEFAULT_SERVICE_PATH = "notes/v1/"; 

我假设我缺少一些注释或配置选项。有谁知道它可能是什么?

请您参考如下方法:

注释中的 scopes 属性未在 Android 身份验证流程中使用,这是它未包含在生成的 Java 代码中的原因之一。相反,Android 使用 ID token ,其中包含足够的信息来向后端表示您的用户/应用程序,而无需请求范围。