C# POST 到带有表单数据的 OAUTH2

C# POST to OAUTH2 with formdata

我已经使用 Chrome Advanced Rest Client (ARC) 为我的 API 调用获取访问令牌。我现在正尝试将我在那里工作的内容转换为我的 SSIS 脚本组件中的 POST 调用。

我有 4 个值需要包含在 post 的数据表单部分中。

grant_type = client_credentials

和我们的 partner_id、client_id 和 client_secret。

How/where 我需要在请求中输入这些信息吗?这是我尝试使用的代码。我有另一个部分执行 GET,当我对令牌进行硬编码时它可以工作。我正在尝试获取此 POST 调用以获取要在 GET 中使用的新令牌。

private TokenObject GetWebServiceToken(string wUrl)

{

    string grant_type = "client_credentials";
    string partner_id = "our partner_id";
    string client_id = "our client_id";
    string client_secret = "our encoded string";

    HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create(wUrl);

    httpWReq.Method = "POST";

    httpWReq.ContentType = "application/x-www-form-urlencoded";

    HttpWebResponse httpWResp = (HttpWebResponse)httpWReq.GetResponse();       

    TokenObject jsonResponse = null;


    try

    {

        //Get the stream of JSON

        Stream responseStream = httpWResp.GetResponseStream();

        //Deserialize the JSON stream

        using (StreamReader reader = new StreamReader(responseStream))

        {   //Deserialize our JSON

            DataContractJsonSerializer sr = new DataContractJsonSerializer(typeof(TokenObject));

            jsonResponse = (TokenObject)sr.ReadObject(responseStream);

        }

    }

    //Output JSON parsing error

    catch (Exception e)

    {

        FailComponent(e.ToString());

    }

    return jsonResponse;
}

可以使用HttpClient吗?我正在使用类似下面的内容从 Salesforce 获取令牌确认。 Json 解析由 Json.net 完成,但任何 json 解析器都应该足够了。

HttpContent content = new FormUrlEncodedContent(new Dictionary<string, string>
{
    {"grant_type", "client_credentials"},
    {"client_id", "<your client id>"},
    {"client_secret", "<your secret>"},
    {"partner_id", "<your partner id>"}
});

using (var httpClient =  new HttpClient())
{
    var message = await httpClient.PostAsync("<your authorization url>", content);
    var responseString = await message.Content.ReadAsStringAsync();

    var obj = JObject.Parse(responseString);

    var oauthToken = (string)obj["access_token"];
    var serviceUrl = (string)obj["instance_url"];

    //more code to write headers and make an actual call
}

application/x-www-form-urlencoded 请求的正文只是以类似于查询字符串参数的方式编码和格式化的文本。

您应该可以通过替换此部分来创建和发送您的请求:

httpWReq.Method = "POST";

httpWReq.ContentType = "application/x-www-form-urlencoded";

HttpWebResponse httpWResp = (HttpWebResponse)httpWReq.GetResponse();   

使用以下代码:

var postData = "grant_type=" + HttpUtility.UrlEncode(grant_type);
postData += "&partner_id=" + HttpUtility.UrlEncode(partner_id);
postData += "&client_id=" + HttpUtility.UrlEncode(client_id);
postData += "&client_secret=" + HttpUtility.UrlEncode(client_secret);

var body = Encoding.ASCII.GetBytes(postData);

httpWReq.Method = "POST";
httpWReq.ContentType = "application/x-www-form-urlencoded";
httpWReq.ContentLength = body.Length;

using (var stream = httpWReq.GetRequestStream())
{
    stream.Write(body, 0, body.Length);
}

HttpWebResponse httpWResp = (HttpWebResponse)httpWReq.GetResponse(); 

// response deserialization logic