我使用谷歌应用程序引擎在gc上部署了应用程序/休息服务,我想在本地从java程序调用服务,我使用基于令牌的身份验证,我能够使用谷歌客户端库检索检索令牌,如果我可以使用带有auth令牌的gc库
看到一些示例调用代码,那将会很棒我的API终点是这样的
https://sam-app-dot-new-dev-merge.appspot.com/api/location/workLocationId?workOrderLocationId=950
(我使用此网址通过邮递员运行服务)
答案 0 :(得分:0)
使用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获得完整响应。
答案 1 :(得分:0)
让我自己回答这个问题
谷歌的文档不清楚(那是我很好)
在你能够运行之前你需要下载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();
}
}
忽略代码评论
答案 2 :(得分:0)
gc cloud sdk configuration
Microsoft Windows [版本10.0.15063] (c)2017 Microsoft Corporation。保留所有权利。
C:\Users\sameer.ali>gcloud -version
错误:(gcloud)论点太少了 用法:gcloud [可选标志] 组可能是app | auth |组件|计算|配置| 容器|数据流| dataproc |数据存储| debug | 部署经理| dns | iam | ml-engine | 组织|项目|服务管理| 来源| sql |话题 命令可能是docker |反馈|帮助|信息| init |版本
有关此命令及其标志的详细信息,请运行: gcloud --help
C:\Users\sameer.ali>gcloud -version
错误:(gcloud)论点太少了 用法:gcloud [可选标志] 组可能是app | auth |组件|计算|配置| 容器|数据流| dataproc |数据存储| debug | 部署经理| dns | iam | ml-engine | 组织|项目|服务管理| 来源| sql |话题 命令可能是docker |反馈|帮助|信息| init |版本
有关此命令及其标志的详细信息,请运行: gcloud --help
C:\Users\sameer.ali>gcloud beta auth application-default login
您当前没有安装此命令组。使用它 需要安装组件:β
重启命令: $ 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:\用户\ sameer.ali&GT;