2017-05-08 52 views
1

我已經使用Chrome高級休息客戶端(ARC)爲我的API調用獲取訪問令牌。我現在試圖把我在那裏工作的東西轉換成我的SSIS腳本組件中的POST調用。C#使用formdata POST到OAUTH2

enter image description here

我有4倍的值,我需要在後的數據形式部分以包括。

grant_type = client_credentials

和我們PARTNER_ID,CLIENT_ID和client_secret。

如何/我在哪裏在我的要求中輸入這些信息?這是我嘗試使用的代碼。我有另一部分做了一個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; 
} 

回答

2

一個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 
+0

謝謝!工作! – Leslie

1

你可以使用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 
}