gpt4 book ai didi

Java Google Contacts API 访问服务帐号认证

转载 作者:行者123 更新时间:2023-12-03 20:18:40 29 4
gpt4 key购买 nike

我正在尝试访问 Googles Contacts API但我的尝试在获得授权时已经失败。从其他(网络)语言,我习惯了 APIConsole 和公共(public) API key (授权)。

GoogleCredential credential = new GoogleCredential().setAccessToken("<<PublicAPIKey>>");
System.out.println(credential.refreshToken()); // false

这样我就无法刷新 token 并且不确定是否将公钥用作 accesstoken ...相反,我尝试了 service account :
private static final String USER_ACCOUNT_EMAIL = "xy@gmail.com";
private static final String SERVICE_ACCOUNT_EMAIL = "xy@developer.gserviceaccount.com";
private static final String SERVICE_ACCOUNT_PKCS12_FILE_PATH = "xy.p12";

public App() {
Set<String> scopes = new HashSet<String>();
scopes.add("https://www.google.com/m8/feeds");

try {
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(new NetHttpTransport())
.setJsonFactory(new JacksonFactory())
.setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
.setServiceAccountScopes(scopes)
.setServiceAccountUser(USER_ACCOUNT_EMAIL)
.setServiceAccountPrivateKeyFromP12File(new java.io.File(SERVICE_ACCOUNT_PKCS12_FILE_PATH))
.build();

System.out.println(credential.refreshToken());
//System.out.println(credential.getAccessToken());
} catch (GeneralSecurityException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

这是我的异常(exception):
com.google.api.client.auth.oauth2.TokenResponseException: 401 Unauthorized
at com.google.api.client.auth.oauth2.TokenResponseException.from(TokenResponseException.java:105)
at com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:287)
at com.google.api.client.auth.oauth2.TokenRequest.execute(TokenRequest.java:307)
at com.google.api.client.googleapis.auth.oauth2.GoogleCredential.executeRefreshToken(GoogleCredential.java:384)
at com.google.api.client.auth.oauth2.Credential.refreshToken(Credential.java:489)
at App.<init>(App.java:50)
at App.main(App.java:29)

感谢您的提示!

最佳答案

无需调用 setServiceAccountUser()我的代码运行良好。但你只会有一个 impersonated account (service_account_mail) 不是您的个人联系人。

“401 Unauthorized”异常的另一个可能来源是离开 credential.refreshToken()。离开。该调用对于将访问代码写入引用是必需的。

在完成的类下面:

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.gdata.client.contacts.ContactsService;

import java.io.File;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.Collections;

public class Connector {

private static ContactsService contactService = null;
private static HttpTransport httpTransport;

private static final String APPLICATION_NAME = "Your-App/1.0";
private static final String SERVICE_ACCOUNT_EMAIL = "xy@developer.gserviceaccount.com";

private Connector() {
// explicit private no-args constructor
}

public static ContactsService getInstance() {
if (contactService == null) {
try {
contactService = connect();

} catch (GeneralSecurityException | IOException e) {
e.printStackTrace();
}
}

return contactService;
}

private static ContactsService connect() throws GeneralSecurityException, IOException {
httpTransport = GoogleNetHttpTransport.newTrustedTransport();

// @formatter:off
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(JacksonFactory.getDefaultInstance())
.setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
.setServiceAccountScopes(Collections.singleton("https://www.google.com/m8/feeds"))
.setServiceAccountPrivateKeyFromP12File(new File("key.p12"))
.build();
// @formatter:on

if (!credential.refreshToken()) {
throw new RuntimeException("Failed OAuth to refresh the token");
}

ContactsService myService = new ContactsService(APPLICATION_NAME);
myService.setOAuth2Credentials(credential);
return myService;
}

}

关于Java Google Contacts API 访问服务帐号认证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29967151/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com