1
votes

I'd appreciate any help with building the OkHttp request to upload an image to flickr.

i've searched stackoverflow, google, yahoo and everywhere else but can't seem to find a solution.

i succeed in completing the oauth authorization process, can search and download image data, etc but am stumped with trying to upload an image using OkHttp. i guess i'd manage to upload the image using plain old httpUrlConnection but i want to do it with OkHttp.

i suspect my problem lies with me not building the request/requestBody in OkHttp correctly (i don't have much knowledge of http, multiforms, etc). this is how i built my request:

    final RequestBody requestBody = new MultipartBuilder()
            .type(MultipartBuilder.FORM)
            .addPart(
                    Headers.of("Content-Disposition", "form-data; name=\"photo\""),
                    RequestBody.create(MEDIA_TYPE_PNG, imageFile))
            .build();

    Request request = new Request.Builder()
            .url(uri.toString())
            .post(requestBody)
            .build();

This is the error i get from flickr:

<rsp stat="fail">
<err code="100" msg="Invalid API Key (Key has invalid format)" />
</rsp>

(by the way i'm able to successfully upload images to imgur).

i doubt that it has anything to do with the API key as this is used successfully with the authorization for search. i think the problem may lie with the way OkHttp "builds the request".

here's what it should look like according to flickr:

If you're constructing the POST query manually instead of using some sort of service library, it should look something like this. All line endings must be \r\n.

POST /services/upload/ HTTP/1.1 Content-Type: multipart/form-data; boundary=---------------------------7d44e178b0434 Host: api.flickr.com Content-Length: 35261

-----------------------------7d44e178b0434 Content-Disposition: form-data; name="api_key"

3632623532453245 -----------------------------7d44e178b0434 Content-Disposition: form-data; name="auth_token"

436436545 -----------------------------7d44e178b0434 Content-Disposition: form-data; name="api_sig"

43732850932746573245 -----------------------------7d44e178b0434 Content-Disposition: form-data; name="photo"; filename="C:\test.jpg" Content-Type: image/jpeg

{RAW JFIF DATA} -----------------------------7d44e178b0434-- The multipart boundary should be randomly generated and should not occur anywhere inside the payload data. Also, don't forget the line ending after your RAW JFIF DATA and before the final boundary.

so my question is, how can i build the OkHttp request to match the above? i only have to include the "photo" parameter, filename and file in building the OkHttp request. all other parameters are included in the uri passed to my request.

thanks in advance

1
thanks. will take a look.Clive Sargeant

1 Answers

0
votes

Not sure that is what you want to do here.

You would use the Flickr API's to make those requests. https://www.flickr.com/services/developer/api/

Any site that requires login you generally have to use their API's to get an authentication token.

Hope this helps.