2

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
yode
  • 26,686
  • 4
  • 62
  • 167
  • Can confirm halirutan's code works, which it should, as it's exactly what's in the API docs. – b3m2a1 Mar 14 '17 at 05:56
  • @MB1965 I just suspect that format of Key – yode Mar 14 '17 at 23:38
  • 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. – b3m2a1 Mar 14 '17 at 23:42
  • 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. – b3m2a1 Mar 14 '17 at 23:52
  • @MB1965 Oh,sorry,I give a wrong image,it should be this.And I don't know the disable related this statement. – yode Mar 14 '17 at 23:54
  • ahhh, I think I know what's up. Read this. If you're blocked from using the service without logging in you may need to get an auth token first. – b3m2a1 Mar 14 '17 at 23:58
  • @MB1965 Are you availabe now here? – yode Mar 15 '17 at 00:04

1 Answers1

3

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.

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
  • 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. – b3m2a1 Apr 05 '17 at 02:30
  • 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. – yode Apr 05 '17 at 02:44