【问题标题】:How to fetch data from google api token?如何从谷歌 api 令牌中获取数据?
【发布时间】:2012-04-24 20:50:58
【问题描述】:

我想在我的网站中使用 Google 登录凭据。所以我正在使用 Google 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

请帮帮我。

提前致谢。

【问题讨论】:

    标签: c# google-api access-token


    【解决方案1】:

    看看这个 .Net 库

    http://www.dotnetopenauth.net/

    这将为您完成所有繁重的 OpenID/OAuth 工作。您只需将其指向 Google 的 OpenID URL。

    请求特定的用户数据(例如电子邮件)很容易并在此处进行了说明:

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

    【讨论】:

    • 您好先生首先非常感谢您回复我。实际上我只使用 Google ID 而不是 Open ID。我想知道我们如何在 Gmail 上重定向用户并将其重定向回我们的网络应用程序以及我们如何获取登录用户数据。例如 First Name 、 Last Name 、Email id 、 ID 等。再次感谢您的回复。
    • Google ID 是 OpenID 的实现。当您使用您的 GMail 帐户连接到 Stack Overflow 时,您可以使用您的 Google ID 登录,然后您会被引导回 Stack Overflow。这是你想要实现的吗?
    • 是的先生,并试图获取用户基本信息,如 ID、FName、LName、Email、UserName 等。我正在使用 .net 框架。请帮助我并尽快回答。谢谢您的回复。
    • 看看 Rick Strahl 的这篇文章 - west-wind.com/weblog/posts/2009/Sep/17/… - 你应该能够修改这篇文章以使用 Google OpenID。
    【解决方案2】:

    在您的 .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 页面。

    【讨论】:

      猜你喜欢
      • 2013-07-20
      • 2017-04-23
      • 1970-01-01
      • 2013-07-21
      • 2021-11-27
      • 2020-07-26
      • 2016-05-20
      • 2017-11-13
      • 1970-01-01
      相关资源
      最近更新 更多