随笔-59  评论-107  文章-1  trackbacks-6

使用Google的Auth Sub登录网站

有关Google AuthSub的文档和相关的内容,请参考:AuthSub Authentication for Web Applications

首先,你的程序必须在Google注册才能调用此接口,因此先到Google的Manage your domains 页面注册你的程序,注册界面如下:

        需要注意以下几点:

        1."Target URL path prefix"只是个前缀而已,并不是完整的回转的路径,在生成登录的时候,还是要将回转的路径发送给Google,而且必须和这里注册的前缀相符,这应该算是用来保障安全的一个设置。

        2.OAuth Consumer Key和OAuth Consumer Secret在OAuth认证的时候相当的重要,不过在AuthSub之中是没有用到的。

        注册完成之后,就要开始生成登录的URL,按照Google的文档,AuthSub的登陆URL应该是https://www.google.com/accounts/AuthSubRequest 加上一些参数组成,我用到了两个参数:

        1.next参数,指定用户登录完成之后回转的URL地址,注意必须以上面注册的"Target URL path prefix"开头;

        2.scope参数,指定需要访问哪些资源,这里的资源是以URL来描述的,例如我们在系统之中需要得到用户的登录用户名,所以要访问用户的通讯录(Google登录回转参数并不包含用户名),因此可以从Google Contacts Data API 上查到需要使用的scopehttp://www.google.com/m8/feeds/,注意,如果需要访问多个资源,则以空格隔开每个URL

    结合以上两个参数(注意,每一个参数都必须经过URL编码),就可以的到一个URL地址,例如https://www.google.com/accounts/AuthSubRequest?next=http%3a%2f%2fwww.12634.com%2fAuth.aspx%3fref%3dgoogle.com&scope=http%3a%2f%2fwww.google.com%2fm8%2ffeeds%2f,我们将用户转向过去,用户就会到达Google的登陆界面(假如用户已经登录到Google,则会跳过此界面):

用户登录之后,就会到Google的帐户信息授权页面,询问用户是否授权系统访问用户的特定信息:


用户只有点击“授予访问权”才会正常登录,在登录完成之后,Google会将用户转向到访问你在上面的next参数之中指定的URL地址,并在地址之中加入一个token参数包含一个访问令牌,例如这个网址:http://www.12634.com/Auth.aspx?ref=google.com&token=1%2Ffm08FqkTrgU1__________jzBqLA


下一步就是根据我们已经得到的token参数获取用户的用户名(E-mail地址),我研究了好久,最后发现发现只需要访问用户的地址本即可,因为用户即使地址本之中没有任何内容,返回的内容也包含他本人的E-mail地址,因此,我通过HttpWebRequest访问如下网址:http://www.google.com/m8/feeds/contacts/default/thin?max-results=0,这个大体含义是以最简单的结果(thin)返回地址本之中的0条记录,当然,访问的时候要使用刚才获取到的token令牌,否则会得到需要认证的提示,具体的方法请参考下面的代码。

代码:

登录链接:

1. 

<a href="https://www.google.com/accounts/AuthSubRequest?next=http%3a%2f%2fwww.12634.com%2fAuth.aspx%3fref%3dgoogle.com&scope=http%3a%2f%2fwww.google.com%2fm8%2ffeeds%2f">Google Login</a>

2. Auth.aspx.cs

    public partial class Auth : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Write("token:" + Request.Params["token"] + "<br />");

            string urlData = "http://www.google.com/m8/feeds/contacts/default/thin?max-results=0";
            string token = Request["token"];
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(urlData));//访问通讯录数据
            request.Headers.Add("Authorization", "AuthSub token=\"" + token + "\"");//这一句将token令牌加入到数据访问请求之中
            request.Method = "GET";
            HttpWebResponse response = (HttpWebResponse)getResponse(request);
            XmlDocument doc = new XmlDocument();
            if (response != null)
            {
                //string txt = new StreamReader(response.GetResponseStream()).ReadToEnd();
                doc.Load(response.GetResponseStream());
                XmlNode node = doc.SelectSingleNode("*/*[local-name()='id']");//读取用户的ID(E-mail地址)
                string account = node != null ? node.InnerText : "";
                node = doc.SelectSingleNode("*/*[local-name()='author']/*[local-name()='name']");//读取用户昵称
                string name = node != null ? node.InnerText : "";

                Response.Write("account:" + account + "<br />");
                Response.Write("name:" + name + "<br />");
            }
        }

        private static HttpWebResponse getResponse(HttpWebRequest request)
        {
            try
            {
                return (HttpWebResponse)request.GetResponse();
            }
            catch (WebException e)
            {
                HttpContext.Current.Response.Write(e.Message);
                string result = new StreamReader(e.Response.GetResponseStream()).ReadToEnd();
                HttpContext.Current.Response.Write(result);
                HttpContext.Current.Response.End();
            }
            return null;
        }
    }

Google Oauth测试工具: http://googlecodesamples.com/oauth_playground/index.php

posted on 2011-07-07 17:24 myx 阅读(125) 评论(0) 编辑 收藏