使用Java为Blogger API验证自己的Google帐户
我想写一个机器人,将本地文件发布到谷歌博客。我将是唯一一个使用此应用程序的人,因此我不需要设置用户友好的身份验证例程。我花了一个晚上的时间试着把事情安排好,但我仍然在为OAuth请求而挣扎 我创建了一个新的Google应用程序项目(类型:installed desktop app),并将Blogger API添加为作用域,然后为我自己的帐户导出了一个客户端机密文件(Google-credentials.json,见下文) 代码: 我还尝试了GoogleAuthorizationCodeFlow+GoogleTokenResponse,但我不知道在哪里注册令牌响应,因为新的GoogleCredential().setFromTokenResponse()似乎是不允许的使用Java为Blogger API验证自己的Google帐户,java,oauth,oauth-2.0,google-api-java-client,Java,Oauth,Oauth 2.0,Google Api Java Client,我想写一个机器人,将本地文件发布到谷歌博客。我将是唯一一个使用此应用程序的人,因此我不需要设置用户友好的身份验证例程。我花了一个晚上的时间试着把事情安排好,但我仍然在为OAuth请求而挣扎 我创建了一个新的Google应用程序项目(类型:installed desktop app),并将Blogger API添加为作用域,然后为我自己的帐户导出了一个客户端机密文件(Google-credentials.json,见下文) 代码: 我还尝试了GoogleAuthorizationCodeFlow
我从谷歌找到的例子是几年前的导入授权CodeInstalledApp,它不是我的依赖项com.Google.api: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());
}
private static Credential authorize()引发异常
{
HttpTransport HttpTransport=GoogleNetHttpTransport.newTrustedTransport();
JsonFactory JsonFactory=JacksonFactory.getDefaultInstance();
List scopes=Arrays.asList(BloggerScopes.BLOGGER);
//加载客户端机密
GoogleClientSecrets clientSecrets=GoogleClientSecrets.load(jsonFactory,新的InputStreamReader(新文件InputStream(clientId_文件));
//设置授权代码流
GoogleAuthorizationCodeFlow=新建GoogleAuthorizationCodeFlow.Builder(
httpTransport,jsonFactory,clientSecrets,scopes).build();
//授权
返回新的AuthorizationCodeInstalledApp(流,新的LocalServerReceiver())。授权(“用户”);
}
受保护的void doTest2()引发异常
{
//配置已安装的应用程序OAuth2流。
凭证=授权();
Blogger Blogger=new Blogger.Builder(httpTransport,jsonFactory,credential).setApplicationName(APP_NAME).setHttpRequestInitializer(credential)
.build();
//请求操作对象。
GetByUrl BloggGetByUrlAction=blogger.blogs().GetByUrl(BLOG_URL);
//配置要查找的博客URL。
setUrl(BLOG_URL);
//将结果内容限制为我们需要的数据。
设置字段(“描述、名称、帖子/totalItems,已更新”);
//此步骤将请求发送到服务器。
Blog Blog=blogGetByUrlAction.execute();
//现在我们可以浏览响应。
System.out.println(“Name:+blog.getName());
System.out.println(“Description:+blog.getDescription());
System.out.println(“帖子计数:+blog.getPosts().getTotalItems());
System.out.println(“上次更新:+blog.getUpdated());
}
我还需要在pom.xml(maven项目)中设置以下依赖项
com.google.api-client
谷歌api客户端
1.20.0
com.google.api-client
google-api-client-java6
1.20.0
com.google.oauth-client
谷歌oauth客户端jetty
1.21.0
com.google.api
谷歌api服务博客
v3-rev50-1.21.0
{
"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."
}
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());
}
<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>