Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

insert new image into google-picasa album using gdata apis through oauth2.0 in nodejs

I tried to insert new image to google-picasa album using Gdata api authenticate via oauth2.0 from request.js node.js module.

My Function:

insertPhoto(options,callback){
    fs.readFile('C:/Users/Public/Pictures/Sample Pictures/Chrysanthemum.jpg',"base64",function(error,data){
        var userId=options.userId || 'default';
        var rootUrl='https://picasaweb.google.com/data/feed/api/user/'+userId+'/albumid/'+options.albumId+'';
        var body_data=gen_multipart('testing.jpg','sss',data,'image/jpeg');
        request({
            method:'POST',
            headers:{ 'GData-Version': '2','Authorization':'Bearer' + ' ' + 'my_access_token',"Content-Type":'multipart/related; boundary="END_OF_PART"','Content-Length':body_data.length,"MIME-version":"1.0"},
            body:body_data,
            uri:rootUrl
        },callback);    
    });   
}

Passing options and callback to my function

insertPhoto({albumId:'5917473565459053457'},function(error,success){
    if(error){
        console.log(error);
    }else{
        console.log(success);
    }
});

The following is my output

{ status: 400, message: 'Not an image.' }

Not an image.

what error is this my header and request body which i made is same as in google documentation.

refer: https://developers.google.com/picasa-web/docs/2.0/developers_guide_protocol#PostPhotos

what i did wrong can any one help me!!

like image 811
sridhar Avatar asked Nov 22 '25 17:11

sridhar


1 Answers

I think the problem is that you use "base64" should be binary

This code seem to do the work for me:

var fs = require('fs');
var request = require('request');

exports.upload = function(fileName, options, callback) {
    fs.readFile(fileName,function(error,data) {
        if (error) {
            callback(error, null, null);
        }
        else {
            console.log('Read file', data.length);

            var token = options.token;
            var userId = options.userId || 'default';
            var rootUrl = 'https://picasaweb.google.com/data/feed/api/user/'+
                userId+'/albumid/'+
                options.albumId+'';
            request({
                method:'POST',
                headers:{ 
                    'GData-Version': '2',
                    'Authorization':'Bearer' + ' ' + token,
                    "Content-Type":'image/jpeg',
                    'Content-Length':data.length,
                    "MIME-version":"1.0"},
                body:data,
                uri:rootUrl
            },callback);
        }
    });
};

And the calling test program:

    var imageUpload = require('./imageUpload');
var parseString = require('xml2js').parseString;

imageUpload.upload('...fileName...', { 
        albumId: '....', 
        userId: '...',
        token: '...'
    }, 
    function(error, response, body) {
        if (body && (response.statusCode === 200 || response.statusCode === 201 || response.statusCode === 202)) {
            parseString(body, function (err, result) {
                console.dir(result);

                if (!err) {
                    console.dir(result.entry['media:group'][0]['media:content'][0].$.url);
                }
                else {
                    console.error('Error', err);
                }
            });
        }
        else {
            console.error('Error', response.statusCode, body);
        }

    });
like image 91
user1929819 Avatar answered Nov 25 '25 11:11

user1929819



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!