Roger Urscheler
Roger Urscheler

Reputation: 774

GMail AddOn CardHeader.setImageUrl with url requiring authorization

My Gmail add-on uses OAuth for a service (similar to the github example). Unlike the GitHub example where the avatar is a non-authenticated URL, the avatars in my add-on require authorization. Since my app is already successfully authorized, I was hoping Google would use that token when loading the image set via CardHeader.setImageUrl.

Is there a way to load and display an image that requires authorization?

What I tried:
- load the image via UrlFetchApp, but then there is no option to display the image using the CardHeader API.

Upvotes: 1

Views: 229

Answers (1)

Roger Urscheler
Roger Urscheler

Reputation: 774

I was able to get it working with:

var headers = {
  Authorization: 'Bearer ' + accessToken
};
var response = UrlFetchApp.fetch(url, {
  method: 'get',
  headers: headers
});
var imageUrl = 'data:image/jpeg;base64,' + Utilities.base64Encode(response.getContent());

// Then creating the CardHeader using the image url
return CardService.newCardHeader()
  .setTitle('My Profile')
  .setImageUrl(imageUrl)
  .build();

Upvotes: 1

Related Questions