gpt4 book ai didi

java - Google Web API可以拥有自己的google资源吗?

转载 作者:行者123 更新时间:2023-12-02 10:52:02 25 4
gpt4 key购买 nike

我有一个 Java Web 服务器,它使用 Google 日历。我正在尝试开发一个具有 N 个日历的 Web 应用程序,其中该 Web 应用程序可以创建自己的日历,可以在其中编辑它并与应用程序的用户共享它。

我的疑问是 Google Web API 是否可以拥有自己的 Google 资源(日历、文档、Spread...)或者只能处理用户的资源。

如果答案是肯定的¿如何创建它?按照 Google Calendars API 并使用 Google API Web 应用凭据,我尝试获取日历列表并创建一个日历,但 OAuth 服务器响应出现 401 未经授权的错误。

我举了一个代码示例:

public class CalendarQuickstart {

private static final String APPLICATION_NAME = "Apps Script";
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
private static final String TOKENS_DIRECTORY_PATH = "tokens";

/**
* Global instance of the scopes required by this quickstart. If modifying these
* scopes, delete your previously saved tokens/ folder.
*/
private static final List<String> SCOPES = Collections.singletonList(CalendarScopes.CALENDAR);
private static final String CREDENTIALS_FILE_PATH = "/ReservaSalaKeys.json";

/**
* Creates an authorized Credential object.
*
* @param HTTP_TRANSPORT
* The network HTTP Transport.
* @return An authorized Credential object.
* @throws IOException
* If the credentials.json file cannot be found.
*/
private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException {
// Load client secrets.
InputStream in = CalendarQuickstart.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));

// Build flow and trigger user authorization request.
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY,
clientSecrets, SCOPES)
.setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
.setAccessType("offline").build();
return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
}

public static void main(String... args) throws IOException, GeneralSecurityException {
// Build a new authorized API client service.
final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
Calendar service = new Calendar.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
.setApplicationName(APPLICATION_NAME).build();

Iterator<CalendarListEntry> it = service.calendarList().list().execute().getItems().iterator();
while(it.hasNext()) {
System.out.println(it.next().getSummary());
}

int numberOfCalendars = service.calendarList().list().size();
service.calendars().insert(
new com.google.api.services.calendar.model.Calendar().setSummary("Calendario " + numberOfCalendars + 1))
.execute();
}

谢谢!!

最佳答案

您的错误与凭据有关。 401 error意味着您使用的访问 token 已过期或无效。主要查看建议的操作:

  • Get a new access token using the long-lived refresh token.
  • If this fails, direct the user through the OAuth flow, as described in Authorizing requests with OAuth 2.0.
  • If you are seeing this for a service account, check that you have successfully completed all the steps in the service account page.

关于java - Google Web API可以拥有自己的google资源吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52097654/

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