从谷歌应用引擎调用get url时获取身份验证令牌

时间:2015-04-29 08:06:36

标签: java google-app-engine google-oauth google-compute-engine

我正在尝试使用Google Cloud的App Identity功能从应用引擎连接我的计算引擎应用。

我不确定从哪里可以提取可用于身份验证的accessToken。我目前收到无效的访问令牌,下面是我用来获取访问令牌的代码:

SamReaderFactory sf = SamReaderFactory.makeDefault();
SamReader sam= sf.open(new File(insam));

在服务器代码(托管在计算引擎上)上,我应该如何验证传入令牌,即我应该将它与

进行比较

以下是我的get call到达时服务器日志的摘录

 URL url = new URL("http://104.197.5.2/MyApp/route?TN=12402735");
 ArrayList < String > scopes = new ArrayList < String > ();
 scopes.add("http://104.197.5.2/MyApp/route?TN=12402735");
 AppIdentityService appIdentity = AppIdentityServiceFactory.getAppIdentityService();

 AppIdentityService.GetAccessTokenResult accessToken = appIdentity.getAccessToken(scopes);

 HttpURLConnection connection = (HttpURLConnection) url.openConnection();
 connection.setDoOutput(true);
 connection.setRequestMethod("GET");
 connection.addRequestProperty("Content-Type", "application/json");
 connection.addRequestProperty("Authorization", "Bearer  " + accessToken.getAccessToken());

 connection.setInstanceFollowRedirects(false);
 BufferedReader in = new BufferedReader(new InputStreamReader(
     connection.getInputStream()));
 String inputLine;
 while ((inputLine = in .readLine()) != null)
     resp.getWriter().println(inputLine); in .close();

0 个答案:

没有答案