【问题标题】:how to Access rest Apis deployed on google cloud /app engine如何访问部署在谷歌云/app引擎上的rest API
【发布时间】:2017-06-01 12:09:10
【问题描述】:

我有一个使用谷歌应用引擎在 gc 上部署的应用程序 /rest 服务,我想在本地从 java 程序调用该服务,我正在使用基于令牌的身份验证,并且我能够使用谷歌客户端库检索令牌,如果我能看到一些使用带有身份验证令牌的 gc 库的示例调用代码,那就太好了

我的 API 端点是这样的

https://sam-app-dot-new-dev-merge.appspot.com/api/location/workLocationId?workOrderLocationId=950

(我使用这个网址通过邮递员运行服务)

【问题讨论】:

  • 如果您在本地调用它,请尝试仅使用相对路径。无需使用 REST

标签: java google-app-engine


【解决方案1】:

使用 rest 模板在内部访问 API 并从中获取响应。

 RestTemplate restTemplate = new RestTemplate();
 ResponseEntity<?> response= restTemplate.exchange ("https://sam-app-dot-new-dev-merge.appspot.com/api/location/workLocationId?workOrderLocationId=950", HttpMethod.GET, null, Void.class);

您将从响应对象中的 api 获得完整响应。

【讨论】:

  • 我需要在通话中使用身份验证令牌
【解决方案2】:

让我自己回答这个问题

google 的文档不清楚(那是我很好)

在你可以运行之前你需要下载 gc sdk 并配置它

https://cloud.google.com/docs/authentication/

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.services.GoogleClientRequestInitializer;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.HttpResponse;
import com.google.api.client.http.HttpTransport;

;

/**
 * 
 * @author sameer.ali
 *
 */
public class AuthenticationManager {

    private  String apiAccessToken  = null;
    private String privateKeyId  = null;

    public  HttpResponse getAuthentication() throws IOException {

        GoogleCredential credential = GoogleCredential.getApplicationDefault();
        apiAccessToken = credential.getAccessToken();
        System.out.println("Token **********" + apiAccessToken);
        credential.getServiceAccountPrivateKey();
        privateKeyId = credential.getServiceAccountPrivateKeyId();
        System.out.println(" privateKeyId *************" + privateKeyId);
        HttpTransport httpTransport= credential.getTransport();
        HttpRequestFactory httReqFactory= httpTransport.createRequestFactory(credential);
        GenericUrl url = new GenericUrl("https://your_url/api/location/workLocationId?workOrderLocationId=9544");
        HttpRequest req = httReqFactory.buildGetRequest(url);
        req.getHeaders().setContentType("application/json");
        HttpResponse response = req.execute();
        System.out.println("service response" + response.getContent().toString());
//      HttpRequestInitializer httpRequestInitializer = new HttpRequestInitializer();
//      httpTransport.createRequestFactory(initializer)
//      
        ///new GoogleAuthorizationCodeFlow().;

        return response;
    }

    public static void main(String[] args){

        AuthenticationManager authM = new AuthenticationManager();
        try{
            HttpResponse response = authM.getAuthentication();
            InputStreamReader reader = new InputStreamReader(response.getContent());
            BufferedReader in = new BufferedReader(reader);

            String readed;
            while ((readed = in.readLine()) != null) {
                System.out.println(readed);
            }



//               GoogleAuthorizationCodeFlow flow =  return new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY,
//                      getClientCredential(), Collections.singleton(CalendarScopes.CALENDAR)).setDataStoreFactory(
//                      DATA_STORE_FACTORY).setAccessType("offline").build();
//                
        //  GoogleAuthorizationCodeFlow flow =  return new GoogleAuthorizationCodeFlow.

        }catch(Exception e){
            e.printStackTrace();
        }
//      String token = authM.getAuthentication();

    }

}

忽略代码 cmets

【讨论】:

    【解决方案3】:
    gc cloud sdk configuration
    

    Microsoft Windows [版本 10.0.15063] (c) 2017 年微软公司。保留所有权利。

    C:\Users\sameer.ali>gcloud -version
    

    错误:(gcloud) 参数太少 用法:gcloud [可选标志] 组可能是应用程序 |认证 |组件 |计算 |配置 | 集装箱 |数据流 |数据处理 |数据存储 |调试 | 部署管理器 |域名解析 |我是 |毫升引擎 | 组织 |项目 |服务管理| 来源 | sql |话题 命令可能是 docker |反馈 |帮助 |信息 |初始化 |版本

    有关此命令及其标志的详细信息,请运行: gcloud --help

    C:\Users\sameer.ali>gcloud -version
    

    错误:(gcloud) 参数太少 用法:gcloud [可选标志] 组可能是应用程序 |认证 |组件 |计算 |配置 | 集装箱 |数据流 |数据处理 |数据存储 |调试 | 部署管理器 |域名解析 |我是 |毫升引擎 | 组织 |项目 |服务管理| 来源 | sql |话题 命令可能是 docker |反馈 |帮助 |信息 |初始化 |版本

    有关此命令及其标志的详细信息,请运行: gcloud --help

    C:\Users\sameer.ali>gcloud beta auth application-default login
    

    您当前没有安装此命令组。使用它 需要安装组件:[beta]

    重启命令: $ gcloud 组件安装测试版

    在新窗口中安装组件。

    安装完成后请重新运行此命令。 $ C:\toolkit\google-cloud-sdk\bin..\lib\gcloud.py beta auth application-default login

    C:\Users\sameer.ali>gcloud beta auth application-default login
    

    您的浏览器已打开访问:

    https://accounts.google.com/o/oauth2/auth?redirect_uri=http%3A%2F%2Flocalhost%3A8085%2F&prompt=select_account&response_type=code&client_id=764086051850-6qr4p6gpi6hn506pt8ejuq83di341hur.apps.googleusercontent.com&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcloud-platform&access_type=offline
    

    凭据保存到文件:[C:\Users\sameer.ali\AppData\Roaming\gcloud\application_default_credentials.json]

    这些凭据将被任何请求的库使用 应用程序默认凭据。

    C:\Users\sameer.ali>

    【讨论】:

      猜你喜欢
      • 2019-01-20
      • 1970-01-01
      • 2021-02-04
      • 2018-10-29
      • 2015-03-04
      • 2018-12-19
      • 2021-08-13
      • 2021-07-30
      • 1970-01-01
      相关资源
      最近更新 更多