Skip to content Skip to sidebar Skip to footer

How To Access Google Photos API With Javascript Using Google-api-javascript-client And Read JSON Data?

I am very new to using API and getting JSON data using OAuth. Could anybody help me? I am trying to access clients google photos and read them. These code snippets are from google

Solution 1:

use this link to get more details

On right side there is button Execute, on click that button you will get all photos , you can also find code just clicking a icon right side square icon of text Try this API, a popup will open, click on JAVASCRIPT Tab , you will find code

https://developers.google.com/photos/library/reference/rest/v1/mediaItems/search


Solution 2:

Accessing Google Photo API with your standard Google Apps Script token

I believe you can use the token that you already have with Google Apps Script.

I did go into the Console and setup the credentials for this project but I'm not using them.

function listImages() {
  var token='';
  var html='';
  var n=0;
  do{
    var params = {muteHttpExceptions:true,headers: {"Authorization": "Bearer " + ScriptApp.getOAuthToken()}};
    var url=Utilities.formatString('https://photoslibrary.googleapis.com/v1/mediaItems?pageSize=100%s',(token!=null)?"&pageToken=" + token:"");
    var resp=UrlFetchApp.fetch(url,params);
    Logger.log(resp);
    var js=JSON.parse(resp.getContentText());
    for(var i=0;i<js.mediaItems.length;i++) {
      html+=Utilities.formatString('<br />%s - File Name: %s<br /><img src="%s" width="265"/>',++n,js.mediaItems[i].filename,js.mediaItems[i].baseUrl);
    }
    token=js.nextPageToken;
  }while(token!=null);
  var userInterface=HtmlService.createHtmlOutput(html).setWidth(1200).setHeight(500);
  //SpreadsheetApp.getUi().showModelessDialog(userInterface, 'Images')//dialog
  SpreadsheetApp.getUi().showSidebar(userInterface);//sidebard
}

Solution 3:

Try This Code

call onAuthPhotoApiLoad function on button click

**also include js of google **

var scopeApi = ['https://www.googleapis.com/auth/photoslibrary', 'https://www.googleapis.com/auth/photoslibrary.readonly', 'https://www.googleapis.com/auth/photoslibrary.readonly.appcreateddata'];

function onAuthPhotoApiLoad() {
    window.gapi.auth.authorize(
        {
            'client_id': "Put Client ID Here",
            'scope': scopeApi,
            'immediate': false
        },
        handlePhotoApiAuthResult);
}


function handlePhotoApiAuthResult(authResult) {
    if (authResult && !authResult.error) {
        oauthToken = authResult.access_token;

               GetAllPhotoGoogleApi();
    }
}


function GetAllPhotoGoogleApi() {
       gapi.client.request({
        'path': 'https://photoslibrary.googleapis.com/v1/mediaItems:search',
        'method': 'POST',
        'body': {
            "filters": {
                "mediaTypeFilter": {
                    "mediaTypes": ["PHOTO"]
                }
            }
        }
    }).then(function (response) {
        console.log(response);     

    }, function (reason) {
        console.log(reason);
    });
}

Post a Comment for "How To Access Google Photos API With Javascript Using Google-api-javascript-client And Read JSON Data?"