java - 谷歌日历 api - 我可以重复使用谷歌的令牌吗?
问题描述
我可以使用来自 FE 的令牌,谷歌授权用户将事件插入后端的谷歌日历吗?流程是:
- 谷歌在前端(firebase 和谷歌)上授权的用户
- 在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();
}
解决方案
行。我已经找到了该怎么做。第一的。使用访问令牌的授权应该在前端。我使用 angular 和 firebase 进行授权。
- 安装
gapi
和gapi.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);
}
推荐阅读
- vue.js - 渲染 vue.js 组件的 id 时出现“无效表达式”错误
- html - Sharepoint 母版页
- c++ - 从(视频)帧英特尔实感获取 RGB 值时出现指针异常
- blockchain - sendSignedTransaction 不给出返回值
- java - BinaryTree 后序输出不正确
- python - 为正则表达式运行 setup.py install ...错误让我发疯
- postgresql - 是否可以使用 '\i' 调用 Postgres SQL 脚本,但将文件作为字符串变量引用?
- c++ - 当 2 个线程共享同一个物理内核时,具有错误共享的 volatile 增量在发布时比在调试时运行得慢
- python - 如果前三个句子包含关键字,如何过滤字符串
- vowpalwabbit - Vowpalwabbit Base Learner - 自适应和规范化