2017-07-24 71 views
0

我實現我的(Java)的Web應用程序谷歌雲打印服務,我按照所有的指令,但在response.Here「所需的證書」仍然得到是我的代碼:谷歌雲打印用戶的認證要求

public static void main(String[] args) { 
    HttpPost printPost = new HttpPost(
      "https://www.google.com/cloudprint/search?output=json"); 
    printPost.setHeader("Authorization", 
      "GoogleLogin auth=" +getAccessToken()); 
    printPost.setHeader("scope", 
      "https://www.googleapis.com/auth/cloudprint"); 
    printPost.setHeader("X-CloudPrint-Proxy", "printingTest"); 
    DefaultHttpClient httpclient = new DefaultHttpClient(); 
    try { 
     HttpResponse printResponse = httpclient.execute(printPost); 
     System.out.println(printResponse.getStatusLine()); 
    } catch (ClientProtocolException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 
public static String getAccessToken() { 

    Credential credential = new GoogleCredential.Builder() 
      .setTransport(new NetHttpTransport()) 
      .setJsonFactory(new JacksonFactory()) 
      .setClientSecrets(
        "client_id", 
        "client_secret").build(); 
    try { 
     credential 
       .setRefreshToken("refresh_token"); 
     credential.refreshToken(); 
     return credential.getAccessToken(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    return null; 

} 

這是一個簡單的搜索命中。

回答

0

在設置授權訪問令牌時,使用「承載」而不是「GoogleLogin auth =」,同時注意帶載體的空間,這很重要