【问题标题】:Google calendar api - can I reuse token form google?谷歌日历 api - 我可以重复使用谷歌的令牌吗?
【发布时间】:2021-10-25 15:37:32
【问题描述】:

我可以使用来自 FE 的令牌,其中用户已获得 Google 的授权,可以将事件插入到后端的 Google 日历中吗? 流程是:

  • 在前端(firebase 和 google)上由 google 授权的用户
  • 在 FE 上填写表格并提交
  • 在后端将数据保存到数据库并在日历上发布事件

我可以重复使用来自 FE 的令牌吗?我该怎么做?

我尝试了几个使用GoogleAuthorizationCodeFlow 的解决方案,每次我得到 HTTP 401 或日志中的授权链接...

当前创建的服务如下:

    protected void initialize() {
    GoogleClientSecrets clientSecrets = createClientSecrets();
    GoogleAuthorizationCodeFlow flow = createGoogleAuthorizationCode(clientSecrets);
    LocalServerReceiver receiver = createReceiver();
    Credential credential = createCredentials(flow, receiver); // here I get link to authorization in google :(
    credential.setAccessToken(getAccessToken());
    service = createCalendarClientService(credential);
}

private GoogleAuthorizationCodeFlow createGoogleAuthorizationCode(GoogleClientSecrets clientSecrets) {
    try {
        return new GoogleAuthorizationCodeFlow.Builder(
                HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
                .setDataStoreFactory(new MemoryDataStoreFactory())
                .setAccessType("online")
                .build();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

private LocalServerReceiver createReceiver() {
    return new LocalServerReceiver.Builder()
            .setPort(port)
            .build();
}

private Credential createCredentials(GoogleAuthorizationCodeFlow flow, LocalServerReceiver receiver) {
    try {
        return new AuthorizationCodeInstalledApp(flow, receiver)
                .authorize("user");
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

private Calendar createCalendarClientService(Credential credential) {
    return new Calendar.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
            .setApplicationName(APPLICATION_NAME)
            .build();
}

【问题讨论】:

  • 您需要一个访问令牌,该令牌已在适当范围内获得授权,以便与 Google 日历一起使用。哪个范围取决于您尝试使用的端点。你检查过样品吗? developers.google.com/calendar/api/quickstart/java
  • 嗨,OP,你能解释一下什么是 FE 吗?我对这个词很陌生。
  • @NaziA 我认为他们的意思是前端
  • 是的,我的意思是前端。我尝试在谷歌日历文档中用作 QuickStart,但在创建 Credential:new AuthorizationCodeInstalledApp(flow, receiver) 时获得了授权链接。在我设置访问令牌之前。我想我应该在构造函数中使用令牌,或者在创建 AuthorizationCodeFlow 时使用令牌,但我不知道如何。

标签: java spring-boot google-calendar-api


【解决方案1】:

好的。我已经找到了该怎么做。 第一的。使用访问令牌的授权应该在前端。我使用 angular 和 firebase 进行授权。

  • 安装gapigapi.auth2
  • 在 google (https://console.cloud.google.com) 上为 Web 客户端 (OAuth 2.0) 创建应用和授权数据
  • 然后我在 google 中授权用户

如下:

private async loginWithGoogle(): Promise<void> {
    await this.initializeGoogleApi();
    const googleAuth = gapi.auth2.getAuthInstance();
    if (!this.isLoggedWithGoogle()) {
      const googleUser = await googleAuth.signIn();
      sessionStorage.setItem(this.GOOGLE_ID_TOKEN, googleUser.getAuthResponse().id_token);
      sessionStorage.setItem(this.GOOGLE_ACCESS_TOKEN, googleUser.getAuthResponse().access_token);
    }
    const credential = firebase.auth.GoogleAuthProvider.credential(this.googleToken);
    await this.angularFireAuth.signInWithCredential(credential);
  }

  private isLoggedWithGoogle(): boolean {
    return !!sessionStorage.getItem(this.GOOGLE_ID_TOKEN);
  }

  private async initializeGoogleApi(): Promise<void> {
    return new Promise(resolve => {
      gapi.load('client', () => {
        gapi.client.init(environment.calendarConfig);
        gapi.client.load('calendar', 'v3', () => {
          resolve();
        });
      });
    });
  }

现在我将带有标头的访问令牌发送到后端,在那里我可以在 google 上插入事件或做任何我想做的事情。

public String insertEvent(Event event, String accessToken) {
    try {
        initialize(accessToken);
        return service.events()
                .insert(CALENDAR_ID, event)
                .execute()
                .getId();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

private void initialize(String accessToken) {
    Credential credential = createCredentials(accessToken);
    service = createCalendarClientService(credential);
}

private Credential createCredentials(String accessToken) {
    return new GoogleCredential().setAccessToken(accessToken);
}

【讨论】:

    猜你喜欢
    • 2021-05-06
    • 1970-01-01
    • 1970-01-01
    • 2016-04-15
    • 1970-01-01
    • 1970-01-01
    • 2012-11-13
    • 1970-01-01
    • 2016-01-03
    相关资源
    最近更新 更多