0

我正在使用谷歌登录自定义网站。在这里我为它写了代码

var sOAuthServiceEndPoint = "https://accounts.google.com/o/oauth2/auth?scope=http://gdata.youtube.com https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email&response_type=token&";
var sOAuthRedirectURL = "http://example.com/testpage/test.html";
var termsAndCondURL = "termsandcondition.html";
var sOAuthClientID = "294016263542.apps.googleusercontent.com"; 
var sAuthenticationURL = sOAuthServiceEndPoint + "redirect_uri=" + sOAuthRedirectURL + "&client_id=" + sOAuthClientID;  

即使我使用以下功能获得了访问令牌

    function fnOnLoad() {
      //alert("Form Loaded");
      var sAccessToken = '';
      var params = {}, queryString = location.hash.substring(1),regex = /([^&=]+)=([^&]*)/g, m;
      while (m = regex.exec(queryString)) {
            params[decodeURIComponent(m[1])] = decodeURIComponent(m[2]);
        }
        if(params.error){
            if(params.error == "access_denied"){
                sAccessToken = "access_denied";
                alert(sAccessToken);
            }
        }else{
                sAccessToken = params.access_token;
                alert(sAccessToken);
        }
        window.opener.fnAuthorisationSuccess(sAccessToken);
        window.close();
    }

它工作成功并重定向到我想要的其他页面。但我的问题是如何检索用户登录名..?

我正在为此使用javascript。

提前致谢

4

1 回答 1

1

这可以在文档中找到。

在您的应用程序获取访问令牌并(如有必要)对其进行验证后,您可以在向 Google API 发出请求时使用该访问令牌。如果访问令牌请求中包含https://www.googleapis.com/auth/userinfo.profile范围,则您可以使用访问令牌通过调用 UserInfo 端点获取用户的基本配置文件信息。

端点: https://www.googleapis.com/oauth2/v1/userinfo

返回基本的用户配置文件信息,包括姓名、用户 ID、性别、出生日期、照片、区域设置和时区。如果请求中存在https://www.googleapis.com/auth/userinfo.email范围,则响应中也将存在用户的电子邮件。如果电子邮件已经过验证,那么还有一个字段表明该电子邮件是经过验证的地址。

于 2013-03-13T06:40:44.603 回答