2013-06-28 92 views
0

我试图与HttpClient的POST请求,但现在看来,因为我取回一个坏的请求响应,说明需要一个CLIENT_ID我的参数不被传送。有人能指出我做错了什么吗?下面是关于代码:获取Instagram的令牌HttpClient的

var authLinkUri = 
      new Uri(@"https://api.instagram.com/oauth/access_token"); 

     // define the request parameters 
     var requestObj = new Models.RequestAccessToken() 
          { 
           client_id = config.ClientId, 
           client_secret = config.ClientSecret, 
           grant_type = "authorization_code", 
           redirect_uri = config.RedirectURI, 
           code = code 
          }; 

     // serialize the obj for transfer 
     var requestObjSer = JsonConvert.SerializeObject(requestObj); 

     // create the content obj 
     var content = new StringContent(requestObjSer, Encoding.UTF8, "application/json"); 

     // make request for auth token 
     var response = await requestToken.PostAsync(authLinkUri, content); 

回答

0

好了,经过多次实验尝试后JSON到Instagram的,我是来这的Instagram不接受JSON,不是在这个端点反正结论。如果我错了,有人纠正我。无论如何,这里是我的解决方案:

// define key,value pair 
     var postValues = new List<KeyValuePair<string, string>> 
          { 
           new KeyValuePair<string, string> 
            ("client_id", 
             config.ClientId), 
           new KeyValuePair<string, string> 
            ("client_secret", 
             config.ClientSecret), 
           new KeyValuePair<string, string> 
            ("grant_type", 
             "authorization_code"), 
           new KeyValuePair<string, string> 
            ("redirect_uri", 
             config.RedirectURI), 
           new KeyValuePair<string, string>("code", code) 
          }; 

     // now encode the values 
     var content = new FormUrlEncodedContent(postValues); 

// make request for auth token 
     var response = await requestToken.PostAsync(authLinkUri, content); 

这对我来说工作完美无瑕。