2
$\begingroup$

My image host returns a link starting with http://. It seems this format is not supported in SE anymore. To avoid this, I want to change the image host to Imgur. But I have failed to call it right.

There is a code write by Arnoud Buzing here:

Import["http://api.imgur.com/2/upload", "XML", 
       "RequestMethod" -> "POST", 
       "RequestParameters" -> {"key" -> apikey, "image" -> image}]

But it does not seem to work anymore.

And this code derived from halirutan

$file = Export["tmp/img.png", Plot[Sin[x], {x, 0, 2 Pi}], "PNG"];
$req = HTTPRequest[
   "https://api.imgur.com/3/image", <|Method -> "Post", 
    "Headers" -> {"Authorization" -> "Client-ID 93d5e807261cc90", 
      "Accept" -> "application/json"}, 
    "Body" -> {"image" -> File[$file], "type" -> "file"}|>];
URLRead[$req];
Import[%]

Actually I'm out of the firewall now and I can upload images using this button, but I get the following error information from halirutan's code:

{status->500,success->False,data->{error->Imgur is temporarily over capacity. Please try again later.}}


This is my Imgur API information for testing. If this question is solved, I will change it maybe.

Client_Name:mmascript
Client_ID:93d5e807261cc90
Client_Secret:04eacd2b7aeb76b551d9c33a02ddbac4a27f8aa4
$\endgroup$
  • $\begingroup$ Can confirm halirutan's code works, which it should, as it's exactly what's in the API docs. $\endgroup$ – b3m2a1 Mar 14 '17 at 5:56
  • $\begingroup$ @MB1965 I just suspect that format of Key $\endgroup$ – yode Mar 14 '17 at 23:38
  • $\begingroup$ what exactly do you mean? That's just saying that we have to specify "image" in the body, but the others are optional. Furthermore copying and evaluating halirutan's code returns a 200 for me, plus success->True in the json output. $\endgroup$ – b3m2a1 Mar 14 '17 at 23:42
  • $\begingroup$ try it with the exact input structure halirutan sent. That is, use a File object and leave "type"->"file". Or you could try encoding your data as base64 which seems to be another acceptable form. $\endgroup$ – b3m2a1 Mar 14 '17 at 23:52
  • $\begingroup$ @MB1965 Oh,sorry,I give a wrong image,it should be this.And I don't know the disable related this statement. $\endgroup$ – yode Mar 14 '17 at 23:54
3
$\begingroup$

Here's a starting point: after getting your own client ID from here, use the following code, which uses URLFetch[] like in this previous answer:

imgurUpload[img_] := Module[{clientID = "93d5e807261cc90", raw},
  raw = ImportString[URLFetch["https://api.imgur.com/3/image", Method -> "POST", 
                              "Headers" -> {"Authorization" -> "Client-ID " <> clientID}, 
                              "MultipartElements" -> {{"image\"; filename=\"test.png", 
                                                       "image/png"} ->
                              ExportString[img, "PNG"]}], "RawJSON"];
  If[raw === $Failed || ! Lookup[raw, "success"], 
     Echo[raw["data", "error"]]; Return[$Failed]];
  raw["data", "link"]]

I'll leave the writing of a version that uses URLRead[]/URLExecute[] to somebody else.

$\endgroup$
  • $\begingroup$ I provided an HTTPRequest version of that here. I think the issue ended up not being Mathematica. Things were failing for him that weren't for me, which could have been related to a message imgur put out about service disruptions around various parts of the globe. $\endgroup$ – b3m2a1 Apr 5 '17 at 2:30
  • $\begingroup$ The code writed by halirutan actully work,but I find the API is blocked in my area after I invite some friends from different city and country to test. $\endgroup$ – yode Apr 5 '17 at 2:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.