3

I am working on an IONIC application. In this app the user will be able to get photos from his google photos account and do some design manipulations on the image he selected.

So for that I want to use the google photos API

I did not find any example on how to accomplish this in IONIC.

So I am looking for some sample code or guid on how to get this done.

=======================================================

UPDATE

I tried to do it like this:

Login to google with: cordova-plugin-googleplus
And request the https://www.googleapis.com/auth/photoslibrary scope
Here is the code:

//Here we do a login.
this.gplus.login({
   'webClientId': '***********',
   'offline': true,
   'scopes': 'profile email https://www.googleapis.com/auth/photoslibrary'
}).then((res) => {
   //after login we try to get the google photos albums
   this.http.get('https://photoslibrary.googleapis.com/v1/albums', {
      responseType: ResponseContentType.Json,
      params:{
         accessToken: res.accessToken,
         pageSize: 50,          
      }
   }).subscribe(res=>{
      console.log('<--- google images res: ', res);
   },err=>{
      console.log('<--- google images err: ', err);
   });
});

Now I get an error 'Expected OAuth 2 access token'
Here is the full error description:

Request is missing required authentication credential. 
Expected OAuth 2 access token, login cookie or other valid authentication credential. 
See https://developers.google.com/identity/sign-in/web/devconsole-project.

==========================================================

UPDATE 2

So after some research I am trying to get the OAuth 2 access token like this:

//Here we do a login.
this.gplus.login({
   'webClientId': '***********',
   'offline': true,
   'scopes': 'profile email https://www.googleapis.com/auth/photoslibrary'
}).then((res) => {
   //after login we need to get the OAuth 2 access
   //I think like this:
   this.http.post('https://www.googleapis.com/oauth2/v4/token', {
      code: res.serverAuthCode,
      client_id: '*****************',
      client_secret: '*************',
      redirect_url: '***************',
      grant_type: 'authorization_code'
   },{
      responseType: ResponseContentType.Json
   }).subscribe(res=>{
      //after we got the OAuth 2 access, we try to get the google photos albums
      let myHeaders= new Headers();
      myHeaders.append('Content-Type', 'application/json');

      this.http.get('https://photoslibrary.googleapis.com/v1/albums', {
         responseType: ResponseContentType.Json,
         params:{
            pageSize: 50,    
            accessToken: {'bearer': res['_body'].access_token},
          },
          headers: myHeaders
        }).subscribe(res=>{
          console.log('<--- google images res: ', res);
        },err=>{
          console.log('<--- google images err: ', err);
        })
   },err=>{
      ......
   })
  }  
}), err => {
   .....
});

But still getting the same error:

Request is missing required authentication credential. 
Expected OAuth 2 access token, login cookie or other valid authentication credential. 
See https://developers.google.com/identity/sign-in/web/devconsole-project.

So now the question is how do is get an OAuth 2 access token ?

Community
  • 1
  • 1
Dushy
  • 375
  • 3
  • 13

0 Answers0