2016-02-12 32 views
2

我想編寫一個將本地文件發佈到Google Blogger的漫遊器。我將是唯一使用此應用程序的人,所以我不需要設置用戶友好的身份驗證例程。我花了一個晚上試圖設置一些東西,而我仍然在努力處理OAuth請求。使用Java爲Blogger API驗證自己的Google帳戶

我創建了一個新的Google應用程序項目(鍵入:安裝的桌面應用程序)並添加了Blogger API作爲範圍,然後爲我自己的帳戶(google-credentials.json,見下文)導出了一個客戶端祕密文件。

代碼:

try 
{ 
    HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); 
    JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); 
    List<String> scopes = Arrays.asList(BloggerScopes.BLOGGER); 

    GoogleClientSecrets secrets = GoogleClientSecrets.load(jsonFactory, new InputStreamReader(Main.class.getResourceAsStream("/google-credentials.json"))); 

    GoogleCredential credential = new GoogleCredential.Builder().setJsonFactory(jsonFactory) 
                   .setTransport(httpTransport) 
                   .setClientSecrets(secrets) 
                   .build(); 

    Blogger blogger = new Blogger.Builder(httpTransport, jsonFactory, credential).setApplicationName("jd34app") 
                       .build(); 

    blogger.blogs().getByUrl("http://jd34blog.blogspot.com").execute(); 
} 
catch (Exception e) 
{ 
    e.printStackTrace(); 
} 

谷歌-credentials.json:

{ 
    "installed": { 
    "client_id": "<removed>", 
    "project_id": "<removed>", 
    "auth_uri": "https://accounts.google.com/o/oauth2/auth", 
    "token_uri": "https://accounts.google.com/o/oauth2/token", 
    "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", 
    "client_secret": "<removed>", 
    "redirect_uris": [ 
     "urn:ietf:wg:oauth:2.0:oob", 
     "http://localhost" 
    ] 
    } 
} 

響應:

com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden 
{ 
    "code" : 403, 
    "errors" : [ { 
    "domain" : "usageLimits", 
    "message" : "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.", 
    "reason" : "dailyLimitExceededUnreg", 
    "extendedHelp" : "https://code.google.com/apis/console" 
    } ], 
    "message" : "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup." 
} 

我也試過GoogleAuthorizationCodeFlow + GoogleTokenResponse,但我不知道由於新的G,在哪裏註冊令牌響應oogleCredential()。setFromTokenResponse()似乎是不允許的。

Google發現的例子有些年了,並且導入了AuthorizationCodeInstalledApp,它不是我的依賴項com.google.apis的一個類:google-api-services-blogger:v3-rev50-1.21.0。

回答

0

我有同樣的問題,我解決這樣的:

private static Credential authorize() throws Exception 
    { 
    HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); 
    JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); 
    List<String> scopes = Arrays.asList(BloggerScopes.BLOGGER); 

    // load client secrets 
    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(jsonFactory, new InputStreamReader(new FileInputStream(clientId_File))); 

    // set up authorization code flow 
    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
    httpTransport, jsonFactory, clientSecrets, scopes).build(); 

// authorize 
    return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user"); 
    } 

protected void doTest2() throws Exception 
    { 
    // Configure the Installed App OAuth2 flow. 
    Credential credential = authorize(); 

    Blogger blogger = new Blogger.Builder(httpTransport, jsonFactory, credential).setApplicationName(APP_NAME).setHttpRequestInitializer(credential) 
      .build(); 

    // The request action object. 
    GetByUrl blogGetByUrlAction = blogger.blogs().getByUrl(BLOG_URL); 

    // Configure which blog URL to look up. 
    blogGetByUrlAction.setUrl(BLOG_URL); 

    // Restrict the result content to just the data we need. 
    blogGetByUrlAction.setFields("description,name,posts/totalItems,updated"); 

    // This step sends the request to the server. 
    Blog blog = blogGetByUrlAction.execute(); 

    // Now we can navigate the response. 
    System.out.println("Name: " + blog.getName()); 
    System.out.println("Description: " + blog.getDescription()); 
    System.out.println("Post Count: " + blog.getPosts().getTotalItems()); 
    System.out.println("Last Updated: " + blog.getUpdated()); 
} 

我還需要設置以下依賴於我的pom.xml(Maven項目)

<dependency> 
    <groupId>com.google.api-client</groupId> 
    <artifactId>google-api-client</artifactId> 
    <version>1.20.0</version> 
</dependency> 
<dependency> 
    <groupId>com.google.api-client</groupId> 
    <artifactId>google-api-client-java6</artifactId> 
    <version>1.20.0</version> 
</dependency> 
<dependency> 
    <groupId>com.google.oauth-client</groupId> 
    <artifactId>google-oauth-client-jetty</artifactId> 
    <version>1.21.0</version> 
</dependency> 
<dependency> 
    <groupId>com.google.apis</groupId> 
    <artifactId>google-api-services-blogger</artifactId> 
    <version>v3-rev50-1.21.0</version> 
</dependency> 
相關問題