src/GoogleInterface.js
/* Upload image to Google Photos and return the ID and URL */
Upload image to Google Photos and return the ID and URL
Upload image to Google Photos and return the ID and URL
async uploadImage(image) {
        let accessToken = (await this.authClient.getAccessTokenAsync()).token;
        let reqOpts = {
            uri: 'https://photoslibrary.googleapis.com/v1/uploads',
            method: 'POST',
            headers: {
                'Content-type': 'application/octet-stream',
                'X-Goog-Upload-File-Name': image.filename,
                'X-Goog-Upload-Protocol': 'raw',
                'Authorization': 'Bearer ' + accessToken,
            },
            body: image.data
        };
        this.eh.log(`Uploading meme ${image.filename}.`);
        let uploadToken = await request(reqOpts);
        accessToken = (await this.authClient.getAccessTokenAsync()).token;
        reqOpts = {
            uri: 'https://photoslibrary.googleapis.com/v1/mediaItems:batchCreate',
            method: 'POST',
            headers: {
                'Content-type': 'application/json',
                'Authorization': 'Bearer ' + accessToken,
            },
            body: {
                'newMediaItems' : [
                    {
                        'description': image.uuid,
                        'simpleMediaItem': {
                            'uploadToken': uploadToken
                        }
                    }
                ]
            },
            json: true
        };
        this.eh.log(`Adding media item ${image.filename}.`);
        let response = await request(reqOpts);
        let mediaItem = response.newMediaItemResults[0].mediaItem;
        return { id: mediaItem.id, url: mediaItem.productUrl };
    }
async uploadImage ( image ) { let accessToken = ( await this . authClient . getAccessTokenAsync ( ) ) . token ; let reqOpts = { uri : 'https://photoslibrary.googleapis.com/v1/uploads' , method : 'POST' , headers : { 'Content-type' : 'application/octet-stream' , 'X-Goog-Upload-File-Name' : image . filename , 'X-Goog-Upload-Protocol' : 'raw' , 'Authorization' : 'Bearer ' + accessToken , } , body : image . data } ; this . eh . log ( ` ${ image . filename } ` ) ; let uploadToken = await request ( reqOpts ) ; accessToken = ( await this . authClient . getAccessTokenAsync ( ) ) . token ; reqOpts = { uri : 'https://photoslibrary.googleapis.com/v1/mediaItems:batchCreate' , method : 'POST' , headers : { 'Content-type' : 'application/json' , 'Authorization' : 'Bearer ' + accessToken , } , body : { 'newMediaItems' : [ { 'description' : image . uuid , 'simpleMediaItem' : { 'uploadToken' : uploadToken } } ] } , json : true } ; this . eh . log ( ` ${ image . filename } ` ) ; let response = await request ( reqOpts ) ; let mediaItem = response . newMediaItemResults [ 0 ] . mediaItem ; return { id : mediaItem . id , url : mediaItem . productUrl } ; }
Upload image to Google Photos and return the ID and URL
Upload image to Google Photos and return the ID and URL
/* First we need to upload the data */ /* Now we need to actually add the image to the user's album */ // Should only be uploading 1 media item at a time