Google 日历 api - 我可以重复使用令牌形式 google 吗?

Google calendar api - can I reuse token form google?

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

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

我尝试了一些使用 GoogleAuthorizationCodeFlow 的解决方案,每次我在日志中收到 HTTP 401 或 link 授权...

当前创建的服务如下所示:

    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();
}

好的。我已经知道该怎么做了。 第一的。使用访问令牌的授权应该在 front-end 上。我使用 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();
        });
      });
    });
  }

现在我将带有 header 的访问令牌发送到 back-end,然后我可以在 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);
}