使用 Java 为 Blogger API 验证自己的 Google 帐户

Authenticate own Google account for Blogger API with Java

我想编写一个将本地文件发布到 Google Blogger 的机器人。我将是唯一一个使用此应用程序的人,因此我不需要设置用户友好的身份验证例程。我花了一个晚上尝试设置,但我仍在努力处理 OAuth 请求。

我创建了一个新的 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();
}

google-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,但我不知道在哪里注册令牌响应,因为新的GoogleCredential().setFromTokenResponse() 似乎不允许。

我发现 Google 中的示例已有多年历史,并且导入了 AuthorizationCodeInstalledApp,它不是我依赖项的 class com.google.apis:google-api-services-blogger:v3-rev50-1.21.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>