问题
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
回答1:
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.
回答2:
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){})
来源:https://stackoverflow.com/questions/33155303/foursquare-missing-file-upload-invalidphotoformat-error-while-uploading-photo