链接的API(身份验证令牌(oauth2))c#.net

 过客松鼠_230 发布于 2023-02-09 10:53

我想知道是否有人可以给我一些使用API​​中的链接的指示。我以为我很怀疑,但是由于某种原因,当尝试获取auth_token时,我只是收到了HTTP 400错误请求。

我的代码就是:

public string redirectUrl = URLTOREDIRECT;

public String apiKey = APIKEY;

public String apiSecret = APISECRET;

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        if (Request.QueryString["code"] != null)
        {
            VerifyAuthentication(Request.QueryString["code"]);
        }
    }
}

protected void Button1_Click(object sender, EventArgs e)
{

        Response.Redirect("https://www.linkedin.com/uas/oauth2/authorization?response_type=code&client_id=apiKey&scope=rw_company_admin&state=DCEEFWF45453sdffef424&redirect_uri=" + HttpUtility.HtmlEncode(redirectUrl));

}

public String VerifyAuthentication(string code)
{
   string authUrl = "https://www.linkedin.com/uas/oauth2/accessToken";

    var sign = "grant_type=authorization_code" + "&code=" + code + "&redirect_uri=" + HttpUtility.HtmlEncode(redirectUrl) + "&client_id=" + _consumerkey + "&client_secret=" + _consumerSecret;
   // var postData = String.Format("grant_type=authorization_code&code={0}&redirect_uri={1}&client_id={2}&client_secret={3}", code, HttpUtility.HtmlEncode(redirectUrl), apiKey, apiSecret);

    HttpWebRequest webRequest = WebRequest.Create(authUrl + "?" + sign) as HttpWebRequest;
    webRequest.Method = "POST";

    //This "application/x-www-form-urlencoded"; line is important
    webRequest.ContentType = "application/x-www-form-urlencoded";

    webRequest.ContentLength = sign.Length;

    StreamWriter requestWriter = new StreamWriter(webRequest.GetRequestStream());
    requestWriter.Write(sign);
    requestWriter.Close();

    StreamReader responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream());

    return responseReader.ReadToEnd().ToString() ;
}

出于测试目的,代码全部位于同一页面中,并且与redirectUrl位于同一页面中。

真的很困惑,尝试了所有变化。

当我被定向链接到允许应用程序时,第一位显然很有效。第二部分获取身份验证令牌失败。

1 个回答
  • 经过长时间的挠头,尤里卡时刻-不需要发送请求中的信息。

        string authUrl = "https://www.linkedin.com/uas/oauth2/accessToken";
    
        var sign = "grant_type=authorization_code&code=" + HttpUtility.UrlEncode(code) + "&redirect_uri=" + HttpUtility.HtmlEncode(redirectUrl) + "&client_id=" + apiKey + "&client_secret=" + apiSecret;
        //byte[] byteArray = Encoding.UTF8.GetBytes(sign);
    
    
        HttpWebRequest webRequest = WebRequest.Create(authUrl + "?" + sign) as HttpWebRequest;
        webRequest.Method = "POST";
        webRequest.ContentType = "application/x-www-form-urlencoded";
    
        Stream dataStream = webRequest.GetRequestStream();
    
        String postData = String.Empty;
        byte[] postArray = Encoding.ASCII.GetBytes(postData);
    
        dataStream.Write(postArray, 0, postArray.Length);
        dataStream.Close();
    
        WebResponse response = webRequest.GetResponse();
        dataStream = response.GetResponseStream();
    
    
        StreamReader responseReader = new StreamReader(dataStream);
        String returnVal = responseReader.ReadToEnd().ToString();
        responseReader.Close();
        dataStream.Close();
        response.Close();
        return returnVal;
    

    2023-02-09 10:55 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有