oauth2.0 更改Chrome的Google帐户,identity.getAuthToken

zte4gxcn  于 2024-01-06  发布在  Go
关注(0)|答案(1)|浏览(202)

我在我的Chrome扩展中使用identity API来验证用户并向我的后端证明他们是谁。这在大多数情况下都很好,我使用getAuthToken来获取OAuth访问令牌,我将其发送到服务器,它用于确认用户的身份。
唯一的问题是:第一次调用getAuthToken时,用户被要求从他们当前登录的帐户列表中选择一个Google帐户。之后对getAuthToken的任何调用都只是重复使用同一个Google帐户,而不提示用户。我想给予用户以后选择不同Google帐户的能力。似乎clearAllCachedAuthTokens正是我所需要的-而且它很有效-但仅当所选的Google帐户不是登录到Chrome的帐户时。在这种情况下,clearAllCachedAuthTokens不会执行任何操作。
我发现的重置getAuthToken的锁定Google帐户的唯一方法是让用户退出Chrome,这很烦人也很尴尬。有更好的方法吗?

hmtdttj4

hmtdttj41#

对于仍然在这里寻找解决方案的人,我能够通过使用launchWebAuthFlow和手动处理OAuth登录来绕过这个限制。
下面的代码片段:

  1. const getGoogleAuthCredential = () => {
  2. return new Promise<ReturnType<typeof GoogleAuthProvider.credential>>(
  3. (resolve, reject) => {
  4. const redirectUri = chrome.identity.getRedirectURL();
  5. const authUrl = `https://accounts.google.com/o/oauth2/auth?client_id=${oauthClientId}&response_type=token&redirect_uri=${encodeURIComponent(
  6. redirectUri
  7. )}&scope=${encodeURIComponent(oauthClientScopes.join(" "))}`;
  8. chrome.identity.launchWebAuthFlow(
  9. { url: authUrl, interactive: true },
  10. (responseUrl) => {
  11. if (chrome.runtime.lastError) {
  12. reject(chrome.runtime.lastError);
  13. }
  14. if (!responseUrl) {
  15. reject("No response URL returned");
  16. return;
  17. }
  18. const params = new URLSearchParams(
  19. new URL(responseUrl).hash.slice(1)
  20. );
  21. const token = params.get("access_token");
  22. if (!token) {
  23. reject("No token found in the response");
  24. return;
  25. }
  26. const credential = GoogleAuthProvider.credential(null, token);
  27. resolve(credential);
  28. }
  29. );
  30. }
  31. );
  32. };

字符串
希望这有帮助!

展开查看全部

相关问题