首页 > 解决方案 > 谷歌日历 api - 我可以重复使用谷歌的令牌吗?

问题描述

我可以使用来自 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();
}

标签: javaspring-bootgoogle-calendar-api

解决方案


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

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

如下:

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

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

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

推荐阅读