Assembla API POST 结果 returns "Bad request" 的 3 步 OAUTH 2.0 身份验证

3 step OATH 2.0 authentication for Assembla API POST request returns "Bad request"

我正在尝试创建一个工具来与 Assembla API 进行通信,但我在获得正确的身份验证时遇到了问题。我正在使用 here 中描述的 3 步程序。 检索 PIN 码目前不成问题,但我确实想让它在未来变得更加智能。我在用 PIN 码交换不记名令牌时遇到问题。

我的代码是:

private void button1_Click(object sender, EventArgs e)
        {
            var url = string.Format("https://api.assembla.com/token");

            var web = (HttpWebRequest)WebRequest.Create(url);

            web.Method = "POST";
            //web.Accept = "application/x-www-form-urlencoded";
            web.ContentType = "application/x-www-form-urlencoded";
            web.Headers["client_id"] = "xxxxx";
            web.Headers["client_secret"] = "xxxxxx";

            var data = string.Format("grant_type=pin_code&pin_code={0}", textBox1.Text); 

            byte[] buffer = Encoding.UTF8.GetBytes(data);
            web.ContentLength = buffer.Length;

            var postData = web.GetRequestStream();

            postData.Write(buffer, 0, buffer.Length);
            postData.Close();

            WebResponse resp = web.GetResponse();

            var sr = new StreamReader(resp.GetResponseStream());

            var result = sr.ReadToEnd().Trim();
        }

这导致网络请求异常:The remote server returns an error: (400) Bad Request. WebResponse resp = web.GetResponse();

行抛出错误

我不确定如何获取有关该错误的更多信息。但是,如果我使用 http://requestmaker.com/ 填写我的凭据和 PIN 码,它 return 是相同的错误代码以及响应 body 说 {"error":"invalid_request","error_description":"'client_id' required."}。我尝试使用 client_id 作为 headers 和硬编码的 requestmaker,如 Assembla 的示例“https://_client_id:_client_secret@api.assembla.com/token?grant_type=pin_code&pin_code=_pin_code”。两者return相同的结果。

这是我第一次对 http 请求做任何事情,所以这对我来说可能是一些愚蠢的概述。 该错误可能是特定于 Assembla 的,但如果某些人body 在我的代码中发现任何明显的错误,我将感谢您提供纠正建议。

有一个或多或少完整的 API in this answer 用于交换访问令牌的 PIN。 API 用于通过 OAuth 获取 imgUr 帐户上传令牌。部分相关功能:

// the PIN previously received 
string myPin = "";

private bool RequestToken(string CliId, string CliSecret)
{
    // request format as per imgUR API - YMMV
    string sReq = string.Format("client_id={0}&client_secret={1}&grant_type=pin&pin={2}",
                CliId, CliSecret, myPin);
    string url = "https://api.imgur.com/oauth2/token/";

    bool myResult = true;

    // create request for the URL, using POST method
    WebRequest request = WebRequest.Create(url);
    request.Method = "POST";

    // convert the request, set content format, length
    byte[] data = System.Text.Encoding.UTF8.GetBytes(sReq);
    request.ContentType = "application/x-www-form-urlencoded";
    request.ContentLength = data.Length;

    // write the date to request stream
    Stream dstream = request.GetRequestStream();
    dstream.Write(data, 0, data.Length);
    dstream.Close();

    // ToDo: 
    // GetResponse() in Try/Catch
    // if there is an exception get the exact error
    // otherwise:
    //    read the stream and parse (json) the result to get access token

    return myResult;      
}

与您所拥有的没有太大区别,但它们都与格式化请求和提交请求有关。至少对于 imgUr,文档非常具体地说明了请求字符串的外观,因此请仔细检查您要访问的任何站点的文档。

请注意,在大多数情况下,结果是 json,需要对其进行解析以获取标记。