0

我正在尝试使用 HttpClient 发出发布请求,但似乎我的参数没有被传输,因为我收到了一个错误的请求响应,说明需要一个 client_id。有人可以向我指出我做错了什么。以下是相关代码:

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);
4

1 回答 1

3

好的,在尝试将 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);

这对我来说完美无缺。

于 2013-06-28T20:05:11.880 回答