皆さん、
を使用して、Web ベースの UI を使用せずにユーザーcom.google.api.client.auth.oauth2.draft10.AccessTokenRequest.ResourceOwnerPasswordCredentialsGrant
を認証する機能の例を探しています。クラスで提供されているサンプル ( https://server.example.com/authorizeをhttps://accounts.google.com/o/oauth2/authに置き換えたもの) を試しましたが、invalid_request 応答が返されました。これは投稿先の正しい URL ですか? リクエストに追加の属性を設定する必要がありますか? このようにスコープを設定しようとしましたが、うまくいきません request.set("scope", " https://www.googleapis.com/auth/calendar ")。また、response_type、grant_typeを設定してみました。どんな助けでも大歓迎です。コードは次のとおりです(Mavenプロジェクトも添付されています):
テスト.java
import com.google.api.client.auth.oauth2.draft10.AccessTokenErrorResponse;
import com.google.api.client.auth.oauth2.draft10.AccessTokenRequest.ResourceOwnerPasswordCredentialsGrant;
import com.google.api.client.auth.oauth2.draft10.AccessTokenResponse;
import com.google.api.client.http.HttpResponseException;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.jackson.JacksonFactory;
public class Test {
public static void main(String[] args) throws Exception {
try {
ResourceOwnerPasswordCredentialsGrant request =
new ResourceOwnerPasswordCredentialsGrant(
new NetHttpTransport(),
new JacksonFactory(),
"https://accounts.google.com/o/oauth2/auth",
"<client_id>",
"<client_secret>",
"<user_username>",
"<user_password>");
AccessTokenResponse response = request.execute();
System.out.println("Access token: " + response.accessToken);
} catch (HttpResponseException e) {
AccessTokenErrorResponse response = e.response.parseAs(AccessTokenErrorResponse.class);
System.out.println("Error: " + response.error);
}
}
}
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.acme</groupId>
<artifactId>google-oauth</artifactId>
<version>1</version>
<name>Google OAuth</name>
<dependencies>
<dependency>
<groupId>com.google.api.client</groupId>
<artifactId>google-api-client</artifactId>
<version>1.4.1-beta</version>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>