如何从谷歌 api 令牌中获取数据

本文关键字:获取 数据 令牌 api 谷歌 | 更新日期: 2023-09-27 18:30:57

我想在我的网站中使用谷歌登录凭据。所以我正在使用谷歌API。我在URL中获得了谷歌API令牌。所以我想获取登录用户数据?我正在使用 C#。成功登录并重定向回我的网站后,我得到了这个URL:-

http://localhost:20885/WebServices/Welcome.aspx#state=/profile&access_token=ya29.AHES6ZRERYieYZIclNxQp3cPeXDnNWMP4IQuhDaj-TO_4wX2eqziRg&token_type=Bearer&expires_in=3600

请帮助我。

提前谢谢。

如何从谷歌 api 令牌中获取数据

看看这个 .Net 库

http://www.dotnetopenauth.net/

这将为您完成所有繁重的OpenID/OAuth工作。你只需要把它指向谷歌的OpenID URL。

请求特定的用户数据(如电子邮件)很容易,解释如下:

https://developers.google.com/accounts/docs/OpenID

在.aspx页面中使用以下 JavaScript:

// First, parse the query string
var params = {}, queryString = location.hash.substring(1),
regex = /([^&=]+)=([^&]*)/g, m;
while (m = regex.exec(queryString)) {
  params[decodeURIComponent(m[1])] = decodeURIComponent(m[2]);
}
// And send the token over to the server
var req = new XMLHttpRequest();
// consider using POST so query isn't logged
req.open('GET', 'http://' + window.location.host + '/public/Google.aspx?' + queryString, true);
req.onreadystatechange = function (e) {
  if (req.readyState == 4) {
    if (req.status == 200) {
      window.location = params['state']
    }
    else if (req.status == 400) {
      alert('There was an error processing the token.')
    }
    else {
      alert('something else other than 200 was returned')
    }
  }
};
req.send(null);

使用它来调用您的相同页面或其他.aspx页面。