【问题标题】:Getting Instagram token with HttpClient使用 HttpClient 获取 Instagram 令牌
【发布时间】:2013-06-28 09:09:31
【问题描述】:

我正在尝试使用 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);

【问题讨论】:

    标签: c# asp.net-mvc instagram


    【解决方案1】:

    好的,经过多次尝试将 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);
    

    这对我来说完美无缺。

    【讨论】:

    • 7 年后仍然是一个可行的解决方案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多