[英]how to Access rest Apis deployed on google cloud /app engine
我有一個使用Google App Engine在gc上部署的應用程序/ rest服務,我想從Java程序本地調用該服務,我正在使用基於令牌的身份驗證,並且能夠使用Google客戶端庫來檢索檢索令牌。很棒,如果我可以使用帶有身份驗證令牌的gc庫看到一些示例調用代碼
我的API終點是這樣的
https://sam-app-dot-new-dev-merge.appspot.com/api/location/workLocationId?workOrderLocationId=950
(我使用此網址通過郵遞員運行服務)
使用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獲得整個響應。
我自己回答這個問題
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();
}
}
忽略代碼注釋
gc cloud sdk configuration
Microsoft Windows [版本10.0.15063](c)2017 Microsoft Corporation。 版權所有。
C:\Users\sameer.ali>gcloud -version
錯誤:(gcloud)參數太少用法:gcloud [optional flags]組可能是app | 授權| 組件| 計算 配置| 容器| 數據流| dataproc | 資料儲存庫| 調試| 部署經理| dns | iam | ml-engine | 組織| 項目| 服務管理| 來源| sql | topic命令可能是docker | 反饋| 幫助| 信息| 初始化 版
有關此命令及其標志的詳細信息,請運行:gcloud --help
C:\Users\sameer.ali>gcloud -version
錯誤:(gcloud)參數太少用法:gcloud [optional flags]組可能是app | 授權| 組件| 計算 配置| 容器| 數據流| dataproc | 資料儲存庫| 調試| 部署經理| dns | iam | ml-engine | 組織| 項目| 服務管理| 來源| sql | topic命令可能是docker | 反饋| 幫助| 信息| 初始化 版
有關此命令及其標志的詳細信息,請運行:gcloud --help
C:\Users\sameer.ali>gcloud beta auth application-default login
您當前沒有安裝此命令組。 使用它需要安裝組件:β
重新啟動命令:$ gcloud Components安裝beta
在新窗口中安裝組件。
安裝完成后,請重新運行此命令。 $ C:\\ toolkit \\ google-cloud-sdk \\ bin .. \\ lib \\ gcloud.py beta auth應用程序-默認登錄
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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.