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