【问题标题】:Imgur error 400 Client.uploadStringImgur 错误 400 Client.uploadString
【发布时间】:2014-01-04 09:50:39
【问题描述】:

我有一个很长的代码要上传到 imgur,尝试对其进行测试,发现了一些错误,并修复了它们。 (也通过这个网站的帮助。)但现在我得到一个错误 400。

这是代码的一部分,也是它发生的地方

public static ImgurToken GetToken(string clientId, string clientSecret, string pin)
        {
            string Url = "https://api.imgur.com/oauth2/token/";
            string DataTemplate = "client_id={0}&client_secret={1}&grant_type=pin&pin={2}";
            string Data = String.Format(DataTemplate, clientId, clientSecret, pin);

            using (WebClient Client = new WebClient())
            {
                string ApiResponse = Client.UploadString(Url, Data); //Error 400
                var Deserializer = new JavaScriptSerializer();
                var Response = Deserializer.DeserializeObject(ApiResponse) as Dictionary<string, object>;

                return new ImgurToken()
                {
                    AccessToken = Convert.ToString(Response["access_token"]),
                    RefreshToken = Convert.ToString(Response["refresh_token"])
                };
            }
        }

我认为我做的 DATA 和 URL 对吗? Imgur API 网站上也有说明。

编辑:刚刚注意到,我的密码是空的。这对吗?
编辑 2:我检查了一下,我的 ClientSecret 显示为红色文本,但我确定它是正确的。

获取密码:

public static string GetPin(string clientId, string clientSecret)
        {
            string Pin = String.Empty;
            string OAuthUrlTemplate = "https://api.imgur.com/oauth2/authorize?client_id={0}&response_type={1}&state={2}";
            string RequestUrl = String.Format(OAuthUrlTemplate, clientId, "pin", "blah");
            return Pin;
        }

【问题讨论】:

    标签: c# imgur


    【解决方案1】:

    在imgur documentation 中,url 没有尾随/。将其更改为 https://api.imgur.com/oauth2/token 以确保确定。

    此外,您可能需要对 Data 字符串进行 url 编码。您可以使用System.Web.HttpUtility.UrlEncode() 来执行此操作。

    http 400 error 通常在请求语法错误时返回,可能是您缺少的pin。只需确保您从用户那里获得的 pin 已添加到 Data 字符串中,而不是 null 或空。

    【讨论】:

    • 是什么?而不是 / 做。我看到它什么也没做。另外,我添加了 GetKey 类,您可以看到它有一个空字符串 Pin。你能用它做点什么吗?
    • @Stepepper 啊,我现在明白了,我已经更新了我的答案
    • 我应该把 HttpUtility 放在哪里?
    • 哦,我用HttpUtility.UrlEncode(Data)对其进行了编码,但它没有解决任何问题。
    • 是的,我做到了。虽然它确实给出了链接的红色文本。
    猜你喜欢
    • 2015-11-01
    • 1970-01-01
    • 2019-07-05
    • 1970-01-01
    • 2021-06-06
    • 1970-01-01
    • 1970-01-01
    • 2021-02-11
    • 2016-03-24
    相关资源
    最近更新 更多