使用oAuth的端点:获取当前的ClientId

时间:2015-09-18 15:10:12

标签: java google-app-engine

我刚开始在Google App Engine中使用Endpoints构建,一切似乎都运行得很好。我的API拒绝非白名单客户端ID并允许少数白名单。

我目前似乎无法工作的一件事就是获取正在调用我的端点的客户端ID。

这似乎微不足道,但我似乎无法找到在哪里看。

2 个答案:

答案 0 :(得分:0)

在python库中,客户端ID是用户对象的一部分。您可以从端点身份验证返回的com.google.appengine.api.users.User实例获取相同的信息。

参考:https://cloud.google.com/appengine/docs/java/endpoints/getstarted/backend/code_walkthrough#oauth_protecting_a_method

答案 1 :(得分:0)

在你的评论中,你提到你真正想要的是确定你当前所使用的应用程序的版本。为了这个或类似的目的,我的所有应用程序引擎项目都共享一个小的Util类,其中包含以下内容:

/**
 * Whether this is a development server or production environment
 */
public static final boolean DEBUG = (SystemProperty.environment.value() != SystemProperty.Environment.Value.Production);

/**
 * AppId this application is running under (works in production environment)
 */
public static final String APP_ID = SystemProperty.applicationId.get();

/**
 * The mail domain for this application
 */
public static final String MAIL_DOMAIN = AppengineEnv.APP_ID + ".appspotmail.com";

要检查本地调试,我可以检查此类的DEBUG常量。要检查您所使用的版本,可以使用GAE app id中的一致命名架构来分析APP_ID常量,例如“myappid_beta”,“myappid_alpha”等。

这就是全部,除非我完全倒退,你想要的不是你的app引擎应用程序实例的版本,而是android应用程序的版本。如果你想要我建议你只需在包含应用程序版本的请求中添加一个字段。

另一个想法是为端点类使用不同的版本。这样做时,您可以将实际代码放入业务逻辑类,并使端点类成为调用该逻辑的存根(包括版本),您可以将其设置为端点类中的常量。这看起来与此类似:

class BusinessLogic {
    public static void doAwesomeStuff(String version, ManyMoreParams){ ... }
}

@Api (
    version = "beta",
    ...
)
class BetaEndpoints {
   private static final String VERSION = "beta",
   @ApiMethod( ... )
   public void doStuff(){
      BusinessLogic.doAwesomeStuff(VERSION, moreparams);
   }
}

@Api (
    version = "alpha",
    ...
)
class BetaEndpoints {
   private static final String VERSION = "alpha",
   @ApiMethod( ... )
   public void doStuff(){
      BusinessLogic.doAwesomeStuff(VERSION, moreparams);
   }
}