issue trying to change to Mashape Api

536 views
Skip to first unread message

Charles McNall

unread,
Jun 28, 2015, 3:15:26 AM6/28/15
to im...@googlegroups.com
I have been trying to convert my image uploading from using api.imgur.com endpoint to the mashape endpoint.
I tried to update the headers to work with both retrofit and also tried to get Unirest to work.
I keep getting  D/Retrofit﹕ {"message":"Invalid Mashape Key"}
I have tried regen the key but still have this error.
I can't even get the unirest version to compile do to an unhandled excemption error.
Anyone know the correct format for Retrofit so the key will work?
Thanks.

Retrofit:
@POST("/3/image")ImageResponse postImage (
@Header("X-Mashape-Key")String key,
@Header("Authorization")String auth,
@Header("Type")String type,
@Query("title")String title,
@Query("description")String description,
@Query("album")String albumId,
@Query("account_url")String username,
@Body TypedFile file

response = imgurAdapter.create(ImgurAPI.class)
.postImage(
Const.getMashapeAuth(), Const.getClientAuth(), type, title, description, albumId, null, new TypedFile("image/*", image)
);

public static String getMashapeAuth() {
return "X-Mashape-Key: " + X_MASHAPE_KEY;
}


Unirest method:
HttpResponse<JsonNode> response = Unirest.post("https://imgur-apiv3.p.mashape.com/3/image")
.header("X-Mashape-Key", Const.X_MASHAPE_KEY)
.header("Authorization", Const.getClientAuth())
.header("Content-Type", "application/x-www-form-urlencoded")
.field("album", albumId)
.field("description", description)
.field("image", image)
.field("title", title)
.asJson();



Charles McNall

unread,
Jul 1, 2015, 1:00:43 AM7/1/15
to im...@googlegroups.com
To those trying to use retrofit this is what i have figured out so far with some help from Imgur staff:
Hi Charles,

Thanks for letting us know about this! It has to be the way the headers are being set. Also, make sure to send the data as form url-encoded data. Here is a working example with cURL and your credentials:

-H "X-Mashape-Key: <its a secret>" \
-H "Authorization: Client-ID <shhhhhhhh don't tell anyone>" \
--data-urlencode "type=url" \
--data-urlencode "image=http://i.imgur.com/M4ljomh.jpg"

Let me know if I can help further!

So I took that and did the following
now I am receiving "400 Bad request"
"data":{"error":"Image format not supported, or image is corrupt.","request":"\/3\/image\/","method":"POST"},"success":false,"status":400}
Which is better, so now i need to figure out if i can get it to accept a Typed file or a direct local file link from the phone or if i need to convert.

@FormUrlEncoded
@POST("/3/image/")ImageResponse postImage (
@Header("X-Mashape-Key")String key,// header seems correct type. otherwise 401 auth error
@Header("Authorization")String auth,
@Header("type")String type, // unsure if this is necessary
@Field("title")String title,
@Field("description")String description,
@Field("album")String albumId,
@Field("account_url")String username,
@Field("image") TypedFile image // unsure on what type of files are acceptable. they are natively .jpg

Log cat like this:
 Content-Type: application/x-www-form-urlencoded; charset=UTF-8
D/Retrofit﹕ Content-Length: 133
D/Retrofit﹕ title=h&description=Get+the+snicKer+app+isnicker.com&image=%2Fstorage%2Femulated%2F0%2FDCIM%2FCamera%2FIMG_20150630_170632739_HDR.jpg
D/Retrofit﹕ ---> END HTTP (133-byte body)

I'll email support again for more help.
anythoughts?

charle...@gmail.com

unread,
Jul 2, 2015, 3:35:20 PM7/2/15
to im...@googlegroups.com
Got it.
Type is: "application/x-www-form-urlencoded"

Post is:


@FormUrlEncoded
@POST("/3/image/")ImageResponse postImage (
@Header("X-Mashape-Key")String key,

@Header("Authorization")String auth,
@Header("Content-Type")String type,


@Field("title")String title,
@Field("description")String description,
@Field("album")String albumId,
@Field("account_url")String username,

@Field("image") String file

requested like:
response = imgurAdapter.create(ImgurAPI.class)
.postImage(
Const.MY_X_KEY_MASHAPE, Const.getClientAuth(),Const.type, title, description, albumId, null , image64
);

And the BIG THING:
convert image to base64 string

try {
InputStream inputStream = new FileInputStream(image);//You can get an inputStream using any IO API
byte[] bytes;
byte[] buffer = new byte[8192];
int bytesRead;
ByteArrayOutputStream output = new ByteArrayOutputStream();
try {
while ((bytesRead = inputStream.read(buffer)) != -1) {
output.write(buffer, 0, bytesRead);
}
} catch (IOException e) {
e.printStackTrace();
}
bytes = output.toByteArray();

String image64 = Base64.encodeToString(bytes, Base64.DEFAULT);

}
catch(Exception e) {
e.printStackTrace();
Log.i(TAG, "Image file not available");
}

Hope that saves someone some time

usmanfa...@gmail.com

unread,
Aug 2, 2016, 12:57:58 PM8/2/16
to Imgur
How to do a GET request then?Can you help me?
Reply all
Reply to author
Forward
0 new messages