2

以下のコードを使用して画像をimgur.comにアップロードすると、http400エラーコードが返されます。私の開発者キーは正しく、最大70kbのサイズのさまざまな画像形式を試しました。http://api.imgur.com/examplesにあるc#のコード例も試しましたが、http400も表示されます。何が問題なのでしょうか。

public XDocument Upload(string imageAsBase64String)
{
    XDocument result = null;
    using (var webClient = new WebClient())
    {
        var values = new NameValueCollection
        {
            { "key", key },
            { "image", imageAsBase64String },
            { "type", "base64" },
        };
        byte[] response = webClient.UploadValues("http://api.imgur.com/2/upload.xml", "POST", values);
        result = XDocument.Load(new MemoryStream(response));
    }
    return result;
}

編集:これはASP.NET MVCアプリケーションであり、呼び出し元のコントローラーのアクションは次のとおりです。

[HttpPost]
public ActionResult UploadImage(HttpPostedFileBase uploadFile)
{
    if (uploadFile.ContentLength > 0)
    {
        var imgService = new ImgUrImageService();
        byte[] fileBytes = new byte[uploadFile.InputStream.Length];
        Int64 byteCount = uploadFile.InputStream.Read(fileBytes, 0, (int)uploadFile.InputStream.Length);
        uploadFile.InputStream.Close();
        string fileContent = Convert.ToBase64String(fileBytes, 0, fileBytes.Length);
        var response = imgService.Upload(fileContent);
    }
    return View();
}
4

2 に答える 2

1

コードを次のように変更した場合:

public XDocument Upload(string imageAsBase64String)
{
    XDocument result = null;
    using (var webClient = new WebClient())
    {
        var values = new NameValueCollection
            {
                { "key", key },
                { "image", imageAsBase64String }
            };
        byte[] response = webClient.UploadValues("http://api.imgur.com/2/upload.xml", "POST", values);
        result = XDocument.Load(System.Xml.XmlReader.Create(new MemoryStream(response)));
    }
    return result;
}

ANONYMOUS API キーを使用すると、すべてが正常に機能します。認証済み API を使用するには、Consumer Key と Consumer Secret を使用して OAuth トークンを作成する必要があります。

Imgur には、必要な特定のエンドポイントに関する詳細情報と、追加のヘルプへのリンクがいくつかあります: http://api.imgur.com/auth

変換コードはほとんど問題ないように見えますが、少し変更しました。

[HttpPost]
public ActionResult UploadImage(HttpPostedFile uploadFile)
{
    if (uploadFile.ContentLength > 0)
    {
        var imgService = new ImgUrImageService();
        byte[] fileBytes = new byte[uploadFile.ContentLength];
        uploadFile.InputStream.Read(fileBytes, 0, fileBytes.Length);
        uploadFile.InputStream.Close();
        string fileContent = Convert.ToBase64String(fileBytes);
        var response = imgService.Upload(fileContent);
    }
    return View();
}

元のアップロード コードで type の値を追加していますが、まだこれを追加していますか、それとも上記の変更したコードに合わせてコードを切り替えましたか? この値を追加する理由がわかりませんし、imgur でサポートされている場所もわかりません。

于 2010-12-05T03:27:05.350 に答える
1

わかりました。理由がわかりました。web.config ファイルのプロキシ設定 (Fiddler 用) が問題の原因でした。それを削除すると問題が解決し、別の問題も解決しました(recaptchaに関連)。コードは魅力のように機能しています。

于 2010-12-15T20:10:06.003 に答える