3

I'm trying to add a photo to foursquare page using api: https://api.foursquare.com/v2/photos/add and following node.js code:

                    var accessToken = "myAccessToken";
                    var platformProfileId = "4squarePageId";
                    var b64content = "somebase64stringrepresentationofimage";
                    var url = "https://api.foursquare.com/v2/photos/add";
                    var formObj = {'oauth_token': accessToken, v: '20151009', 'pageId': platformProfileId, 'photo': b64content};
                    request({
                        url: url, //URL to hit
                        form: formObj, //form data
                        method: 'POST',
                        headers: { 'Content-Type': 'image/jpeg' }
                    }, function(error, response, body){
                        if(error) {
                            console.log(error);
                            return cb(error);
                        } else {
                            if(typeof body != 'object') {
                                body = JSON.parse(body);
                            }
                            console.log(body);
                            if(('meta' in body) && ('code' in body['meta']) && (body['meta']['code'] != 200)) {
                                return callback_inner("error");
                            }
                            var mediaIdStr = body['response']['id'];
                            return callback_inner(null, mediaIdStr);
                        }
                    });

I'm getting following response:

{ meta: 
   { code: 400,
     errorType: 'other',
     errorDetail: 'Missing file upload',
     requestId: '561fe6c1498e097824456e38' },
  notifications: [ { type: 'notificationTray', item: [Object] } ],
  response: {} }

Can anyone please tell me where am I doing wrong ?

Update:

                             var queryObj = {'oauth_token': accessToken, v: '20151009', 'pageId': platformProfileId};
                             request({
                                url: url, //URL to hit
                                qs: queryObj, //query obj
                                method: 'POST',
                                headers: { 'Content-Type': 'image/jpeg' },
                                body: b64content
                            }, function(error, response, body){
                                if(error) {
                                    console.log(error);
                                    return cb(error);
                                } else {
                                    if(typeof body != 'object') {
                                        body = JSON.parse(body);
                                    }
                                    console.log(body);
                                    if(('meta' in body) && ('code' in body['meta']) && (body['meta']['code'] != 200)) {
                                        return callback_inner("error");
                                    }
                                    var mediaIdStr = body['response']['id'];
                                    return callback_inner(null, mediaIdStr);
                                }
                            });

Tried sending image as post message body but even then it's not working.

Update 2:

                var  b64mediaFilesArr = results.C;
                async.map(b64mediaFilesArr, function(b64content, callback_inner){
                    var imagename = new Date() + '.jpg';
                    var url = "https://api.foursquare.com/v2/photos/add";
                    var formObj = {
                        'oauth_token': accessToken, 
                        'v': '20151009', 
                        'pageId': platformProfileId, 
                        'photo': {
                            value: b64content,
                            options: {
                                filename: imagename,
                                contentType: 'image/jpeg'
                            }
                        }
                    };
                    request({
                        url: url, //URL to hit
                        formData: formObj, //form data
                        method: 'POST',
                        headers: { 'Content-Type': 'image/jpeg' }
                        }, function(error, response, body){
                        if(error) {
                            console.log(error);
                            return cb(error);
                        } else {
                            if(typeof body != 'object') {
                                body = JSON.parse(body);
                            }
                            console.log(body);
                            if(('meta' in body) && ('code' in body['meta']) && (body['meta']['code'] != 200)) {
                                return callback_inner("error");
                            }
                            var mediaIdStr = body['response']['id'];
                            return callback_inner(null, mediaIdStr);
                        }
                    }); 

If I use above code, then there is change in the response:

{ meta: 
   { code: 400,
     errorType: 'param_error',
     errorDetail: 'InvalidPhotoFormat: Unable to determine photo type',
     requestId: '56207798498ee45703ab6059' },
  notifications: [ { type: 'notificationTray', item: [Object] } ],
  response: {} }

I'm going crazy after this. Can anyone please help me out ?

Solution

In addition to below accepted answer, I solved base64 encoded problem. For those of you using base64 encoded image data in your web app, you need to send original binary rep of image to Foursquare. This SO answer helped me to do that. Convert Binary.toString('encode64') back to Binary

Community
  • 1
  • 1
dark_shadow
  • 3,503
  • 11
  • 56
  • 81

2 Answers2

5

These are the request options that worked for me:

var options = {
    'url': 'https://api.foursquare.com/v2/photos/add',
    'qs': {
        'v': '20161001',
        'oauth_token': ACCESS_TOKEN,
        'venueId': VENUE_ID
    },
    'formData': {
        'file': {
            'value': RAW_IMAGE_BUFFER,
            'options': {
                'filename': 'topsecret.jpg',
                'contentType': 'image/jpg'
            }
        }
    },
    'json': true
};

Then just call:

request.post(options, function(error, response, body){})
crispmark
  • 71
  • 1
  • 6
2

photo parameter does not exist. photo is response field.

The image data is sent as the POST message body on the HTTP request.

EDIT

You use request? Refer to https://github.com/request/request#multipartform-data-multipart-form-uploads

You don't need encode into base64.

Kosuke Ogawa
  • 7,383
  • 3
  • 31
  • 52
  • so which param should I use for sending image data ? – dark_shadow Oct 16 '15 at 02:56
  • The photo is NOT passed in one of the parameters. The image data is sent as the POST message body on the HTTP request. See http://en.wikipedia.org/wiki/HTTP_request#Request_message for more information on HTTP requests. – Kosuke Ogawa Oct 16 '15 at 03:05
  • I tried sending image data in body but even then it's not working. Please take a look at my updated code – dark_shadow Oct 16 '15 at 03:15
  • @dark_shadow Why do you encode into base64? I edited my answer. – Kosuke Ogawa Oct 16 '15 at 04:49
  • I'm using gridfs to store images. When I read the file I read it as base64 encoded string. Is there no way I can send the base64encoded image representation to foursquare ? – dark_shadow Oct 16 '15 at 05:18
  • Which encoding should I use then ? – dark_shadow Oct 16 '15 at 05:22
  • This SO thread is using base64 encoded string http://stackoverflow.com/questions/18586466/foursqaure-photo-add-against-checkin but I'm not able to understand why is it not working for me ? – dark_shadow Oct 16 '15 at 07:32
  • I have found solution to my problem. I had to send the original binary string to Foursquare. – dark_shadow Oct 16 '15 at 19:46