2017-05-08 7 views
1

私はAPI呼び出しのアクセストークンを取得するためにChrome Advanced Rest Client(ARC)を使用しました。私はそこで働いているものを取り出し、SSIS Script ComponentでPOST呼び出しに変換しようとしています。フォームデータでOAUTH2へのPOST

enter image description here

私はポストのデータ形式セクションに含める必要がある4つの値を有します。

grant_type = client_credentials

と私たちのPARTNER_ID、CLIENT_IDおよびclient_secret。

どのようにしてこの情報をリクエストに入力しますか?ここで私が使用しようとしているコードです。私はトークンをハードコードするときに動作するGETを行う別のセクションを持っています。私はGETで使用する新しいトークンを取得するためにこのPOST呼び出しを取得しようとしています。

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 
} 
関連する問題