简单做了一个sina微博的MVC第三方登陆

实现的效果是通过第三方sina登陆获取用户信息

现在我们讲述一下认证的步骤

1、第一步先获取sina认证的appkey,appSecret在地址http://open.weibo.com/wiki/中获取,在获取appkey,appSecret的时候同时还要设置RedrictURL


2、获取appkey,appSecret之后并且设置你的redirectURL现在我们就来获取code

        string code = "";
           var ur = Request.Url;
           string mur = ur.ToString();
           var oauth = new NetDimension.Weibo.OAuth("your appkey ", "your appSecret");
           oauth.CallbackUrl = "redirectURL";
           if (mur.IndexOf("code") < 0)
           {
              
             
               var url = oauth.GetAuthorizeURL();
               //    Response.Redirect(url);
               ViewBag.URL = url;

           }

code返回的形式是your redirectURL&code的形式返回的例子http://****?code=asdsxxx,code后面的就是你获取access_token所要的code

3、获取access_token

  code = mur.Substring(mur.IndexOf("code") + 5, mur.Length - mur.IndexOf("code")-5);
               WeiboParameter[] webpara = new WeiboParameter[] { new WeiboParameter("client_id","3487061422"),
                    new WeiboParameter("redirect_uri", "your redirect_url"),
                 new WeiboParameter("client_secret","appSecret"),
                 new WeiboParameter("code", code),//此code是上面获取的code
                 new WeiboParameter("grant_type", "authorization_code")};//设置为默认值就这样写不要改动
               Client mClient = new Client(oauth);
           
             AccessToken aToken = oauth.GetAccessTokenByAuthorizationCode(code);//这就是我们需要的access——token
               saveCookies("ExpiresIn", aToken.ExpiresIn.ToString());
             //   saveCookies("RefreshToken", aToken.RefreshToken.ToString());
               saveCookies("Token", aToken.Token.ToString());
               saveCookies("UID", aToken.UID.ToString());
    public class AccessToken
    {
        [JsonProperty(PropertyName = "access_token")]
        public string Token{get; set;}
        [JsonProperty(PropertyName = "expires_in")]
        public int ExpiresIn{get; set;}
        [JsonProperty(PropertyName = "uid")]
        public string UID{get; set;}
        [JsonProperty(PropertyName = "refresh_token")]
        public string RefreshToken { get;  set; }

    }

4、获取信息,下面我们先看一下获取用户信息的接口http://open.weibo.com/wiki/API%E6%96%87%E6%A1%A3_V2这是api文档,获取用户信息的接口是http://open.weibo.com/wiki/2/users/show

由截图我们可以看到这个用oauth2.0的话只需要access_token和uid即可

  
               WeiboParameter[] webpara1 = new WeiboParameter[] { new WeiboParameter("access_token", aToken.Token), new WeiboParameter("uid",aToken.UID) };
               string content = mClient.GetCommand("https://api.weibo.com/2/users/show.json", webpara1);


               var dynamicJson1 = DynamicJson.Parse(content);
               muser.id = dynamicJson1.id;
               muser.screen_name = dynamicJson1.screen_name;
               muser.name = dynamicJson1.name;
               muser.province = dynamicJson1.province;
               muser.location = dynamicJson1.location;

就这样获取用户信息的例子完成了,这个看明白的话其他的第三方认证都是采用oauth2规则,1、先获取code 2、在获取access_token 3、调用api接口就行了,

希望对大家有所帮助

 

 
posted @ 2012-10-23 20:51  win_and_first  阅读(1375)  评论(1编辑  收藏  举报