LinkedIn OAuth2 redirect_uri

时间:2015-02-06 09:50:19

标签: c# linkedin

我有一个问题。 我试图通过具有一些权限的linkedin获得授权。 在OAuth 2重定向网址列表中,我编写了我想要将重定向重定向到的网址,它是本地测试的本地主机网址:

http://localhost:9000/account/ExternalLogin.aspx 

但是当我创建CustomLinkedInClient的实例:OAuth2Client时,重定向网址变为

http://localhost:9000/account/ExternalLogin.aspx?__provider__=linkedIn&__sid__=xxx

我不确定__provider__=linkedIn&__sid__=xxx是什么,我担心redirect_uri在两次调用之间不匹配。 如果有人可以帮助我,我真的很感激! 谢谢!

这是我发送给linkedin的信息:

https://www.linkedin.com/uas/oauth2/authorization?response_type=code&client_id=xxx&scope=r_basicprofile%20r_emailaddress%20r_fullprofile&state=linkedIn&redirect_uri=http://localhost:9000/account/RegisterExternalLogin.aspx?__provider__=linkedIn&__sid__=xxx

并显示具有权限的linkedin登录信息。但是当我尝试使用linkedin用户进行身份验证时,它会失败!

编辑:

这是我的请求方法:

    public override AuthenticationResult VerifyAuthentication(HttpContextBase context, Uri returnPageUrl)
    {

        string code = context.Request.QueryString["code"];
        if (string.IsNullOrEmpty(code))
        {
            return AuthenticationResult.Failed;
        }
        string accessToken = this.QueryAccessToken(returnPageUrl, code);
        if (accessToken == null)
        {
            return AuthenticationResult.Failed;
        }
}

然后我调用QueryAccessToken,在最后一个请求中返回代码:

 protected override string QueryAccessToken(Uri returnUrl, string authorizationCode)
    {
         var postData = System.Web.HttpUtility.ParseQueryString(string.Empty);
         postData.Add(new NameValueCollection
         {
         { "grant_type", "authorization_code" },
         { "code", authorizationCode },
         { "client_id", _clientId },
         { "client_secret", _clientSecret },
         { "redirect_uri", returnUrl.GetLeftPart(UriPartial.Path) },
         });

         var webRequest = (HttpWebRequest)WebRequest.Create(TokenEndpoint);

         webRequest.Method = "POST";
         webRequest.ContentType = "application/x-www-form-urlencoded";

         using (var s = webRequest.GetRequestStream())
         using (var sw = new StreamWriter(s))
         sw.Write(postData.ToString());

         using (var webResponse = webRequest.GetResponse())
         {
             var responseStream = webResponse.GetResponseStream();
             if (responseStream == null)
             return null;

             using (var reader = new StreamReader(responseStream))
             {
                 var response = reader.ReadToEnd();
                 var json = JObject.Parse(response);
                 var accessToken = json.Value<string>("access_token");
                 return accessToken;
             }
         }

    }

但在尝试访问请求时遇到错误:

远程服务器返回错误:(400)错误请求。

可能是什么? 感谢

编辑2:

新请求方法:

        protected override string QueryAccessToken(Uri returnUrl, string authorizationCode)
        {
            StringBuilder postData = new StringBuilder();
            postData.AppendFormat("client_id={0}", this._clientId);
            postData.AppendFormat("&redirect_uri={0}", HttpUtility.UrlEncode(returnUrl.ToString()));
            postData.AppendFormat("&client_secret={0}", this._clientSecret);
            postData.AppendFormat("&grant_type={0}", "authorization_code");
            postData.AppendFormat("&code={0}", authorizationCode);

            string response = "";
            string accessToken = "";

            var webRequest = (HttpWebRequest)WebRequest.Create(TokenEndpoint);

            webRequest.Method = "POST";
            webRequest.ContentType = "application/x-www-form-urlencoded";

            try
            {

                using (Stream s = webRequest.GetRequestStream())
                {
                    using (StreamWriter sw = new StreamWriter(s))
                        sw.Write(postData.ToString());
                }

                using (WebResponse webResponse = webRequest.GetResponse())
                {
                    using (StreamReader reader = new StreamReader(webResponse.GetResponseStream()))
                    {
                        response = reader.ReadToEnd();
                    }
                }

                JavaScriptSerializer deserializer = new JavaScriptSerializer();
                var userData = deserializer.Deserialize<Dictionary<string, string>>(response);

                //var json = JObject.Parse(response);
                accessToken = (string)userData["access_token"];
            }
            catch (Exception)
            {
                return null;
            }

            return accessToken;
}

0 个答案:

没有答案