У меня возникли проблемы с настройкой моей программы для совершения вызовов API Google Таблиц. В настоящее время я санкционирование моего мандата таким образом:API Google Таблиц API OAuth 2.0
public static Credential authorize() throws IOException, GeneralSecurityException {
InputStream p12 = GoogleUtils.class.getResourceAsStream("/key.p12");
File file = new File("hi");
OutputStream outputStream = new FileOutputStream(file);
IOUtils.copy(p12, outputStream);
outputStream.close();
// Build flow and trigger user authorization request.
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(HTTP_TRANSPORT)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId("8429209348-1nfuorcfko5pmqh2l0[email protected]")
.setServiceAccountScopes(SCOPES)
.setServiceAccountPrivateKeyFromP12File(file)
.build();
return credential;
}
Но, я не знаю, что делать с этим верительным теперь, когда у меня есть. В другом классе я открываю SpreadsheetService
, но я не могу найти способ фактического авторизации с объектом Credential
. Я помню, как делал это в одном из моих старых проектов:
public GoogleDrive() {
/** Our view of Google Spreadsheets as an authenticated Google user. */
try {
InputStream p12 = BotPanel.class.getResourceAsStream("/key.p12");
File file = new File("hi");
OutputStream outputStream = new FileOutputStream(file);
IOUtils.copy(p12, outputStream);
outputStream.close();
String emailAddress = "[email protected]ccount.com";
HttpTransport httpTransport = new NetHttpTransport();
JacksonFactory jsonFactory = new JacksonFactory();
String[] SCOPESArray = { "https://spreadsheets.google.com/feeds", "https://spreadsheets.google.com/feeds/spreadsheets/private/full", "https://docs.google.com/feeds" };
final List<String> SCOPES = Arrays.asList(SCOPESArray);
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport).setJsonFactory(jsonFactory)
.setServiceAccountId(emailAddress).setServiceAccountScopes(SCOPES)
.setServiceAccountPrivateKeyFromP12File(file)
.build();
file.delete();
service = new SpreadsheetService("Test");
service.setOAuth2Credentials(credential);
} catch (Exception e) {
e.printStackTrace();
}
}
Как вы можете видеть, что я получаю Credential
точно так же, и в конце концов я могу просто позвонить service.setOAuth2Credentials(credential);
Но в моей новой проекте, это что я получаю:
Как вы можете видеть, что это не метод называется .setOAuth2Credential(...)
больше.
Копаясь в коде src, я нашел метод в классе GoogleService
, только у него была аннотация @Beta.
@Beta
public void setOAuth2Credentials(Credential credential) {
GoogleAuthTokenFactory googleAuthTokenFactory = getGoogleAuthTokenFactory();
googleAuthTokenFactory.setOAuth2Credentials(credential);
requestFactory.setAuthToken(authTokenFactory.getAuthToken());
}
Есть ли причина, почему этот код аннотируется с помощью @Beta? Есть ли другой способ, которым я должен разрешать вызовы на листы api? Или есть способ разрешить этот метод? (это было бы идеально).