2017-06-01 51 views
0

我有一個應用程序/休息服務部署在gc使用谷歌應用程序引擎,我想調用從本地Java程序服務,我使用基於令牌的身份驗證,我我能夠檢索使用谷歌客戶端庫檢索令牌,這將是巨大的,如果我可以看到使用GC庫的身份驗證令牌如何訪問休息Apis部署在谷歌雲/應用程序引擎

我的API終點一些樣本調用代碼是這樣的

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

(我使用這個URL通過郵遞員運行服務)

+0

如果您在本地調用它,請嘗試僅使用相對路徑。無需使用REST –

回答

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響應對象全響應。

+0

我需要在通話中使用身份驗證令牌 – Sam2016

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(); 

    } 

} 

忽略代碼註釋

0
gc cloud sdk configuration 

Microsoft Windows [版本10.0.15063] (c)2017 Microsoft Corporation。版權所有。

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

錯誤:(gcloud)參數太少 用法:gcloud [可選標誌] 組可能是應用程序| auth |組件|計算|配置| 集裝箱|數據流| dataproc |數據存儲|調試| deployment-manager | dns | iam | ml-engine | 組織|項目|服務管理| 來源| sql |主題 命令可能是docker |反饋|幫助| info | init |版本

有關該命令及其標誌的詳細信息,運行: gcloud --help

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

ERROR:(gcloud)參數太少 用法:gcloud [可選標誌] 基團可以是應用| auth |組件|計算|配置| 集裝箱|數據流| dataproc |數據存儲|調試| deployment-manager | dns | iam | ml-engine | 組織|項目|服務管理| 來源| sql |主題 命令可能是docker |反饋|幫助| info | init |版本

有關此命令及其標誌的詳細信息,請運行: gcloud --help

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

您目前沒有安裝此命令組。使用它 需要組件的安裝:[測試]

重新啓動命令:$ 組件gcloud安裝測試版

在新窗口中安裝的組件。

安裝完成後請重新運行該命令。 $ C:\工具\谷歌雲-SDK \ BIN .. \ LIB \ gcloud.py公測身份驗證應用程序默認的登錄

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>

相關問題