0

I have built a GSM add on and published it for my domain. I built the code, on Google Apps Script and set it up in Google API Console. I installed it for my domain, but it does not show the card in Gmail It should show in the sidebar, and in the compose window. It works fine when I install the head version from within google apps script, but then I publish it to GSM for users in my organization it doesn't work. The purpose of the addon is to collect information from fields in a card and use that in an email template. I think it might be something to do with OAuth scopes but I am not sure. This is my very first GSM project and I don't know what OAuth scopes I should declare in my code and in the Google API console.

Here is my code, I have 2 files, appscript.json, and code.gs. code.gs:

function onGmailCompose(e) {
  console.log(e);
  var header = CardService.newCardHeader()
      .setTitle('Use Template')
      .setSubtitle('Use the template for sending an email after a review has been published.');
  // Create text input for entering the cat's message.
  var input2 = CardService.newTextInput()
  .setFieldName('FName')
  .setTitle('First Name')
  .setHint('What is the readers first name?');
  var input3 = CardService.newTextInput()
  .setFieldName('BookTitle')
  .setTitle('Reviewed Book Title')
  .setHint('What is the title of the book reviewed?');
  var input4 = CardService.newTextInput()
  .setFieldName('BookAuthor')
  .setTitle('Reviewed Book Author')
  .setHint('Who is the author of the book reviewed?');
  // Create a button that inserts the cat image when pressed.
  var action = CardService.newAction()
      .setFunctionName('useTemplate');
  var button = CardService.newTextButton()
      .setText('Use Template')
      .setOnClickAction(action)
      .setTextButtonStyle(CardService.TextButtonStyle.FILLED);
  var buttonSet = CardService.newButtonSet()
      .addButton(button);
  // Assemble the widgets and return the card.
  var section = CardService.newCardSection()
      .addWidget(input2)
      .addWidget(input3)
      .addWidget(input4)
      .addWidget(buttonSet);
  var card = CardService.newCardBuilder()
      .setHeader(header)
      .addSection(section);
  return card.build();
}
function useTemplate(e) {
  console.log(e);
  var FName = e.formInput.FName;
  var Title = e.formInput.BookTitle;
  var Author = e.formInput.BookAuthor;
  var now = new Date();
  var htmlIntro = '<p>Hello, ';
var html2 = ' Thank you for writing a book review at <a href="https://www.goodbookreviews.page">Good Book Reviews</a> on ';
var html3 = ' by ';
var html4 = '. You Review has been published to our site. Any personal information you included was NOT published, including first name, last name,  age, and email address. Only info you wrote about the book was published. You can see it right <a href="https://www.goodbookreviews.page./books-and-reviews/look-at-reviews"> here!</a> If you need anything else, feel free to contact us at support@goodbookreviews.page or reply to this email to contact us. <br> Happy Reading,<br> The Book Review Team</p>';
var message = htmlIntro + FName + html2 + Title + html3 + Author + html4;
  var response = CardService.newUpdateDraftActionResponseBuilder()
  .setUpdateDraftBodyAction(CardService.newUpdateDraftBodyAction()
                            .addUpdateContent(message, CardService.ContentType.MUTABLE_HTML)
                            .setUpdateType(CardService.UpdateDraftBodyType.IN_PLACE_INSERT))
  .build();
  return response;
}
function onGmailMessage(e) {
  console.log(e);
  var header = CardService.newCardHeader()
  .setTitle('Unavailable')
  .setSubtitle('Open the compose window to use template');
  var card = CardService.newCardBuilder()
  .setHeader(header);
  return card.build();
}

appscript.json:

{
  "timeZone": "America/Chicago",
  "dependencies": {
  },
  "exceptionLogging": "STACKDRIVER",
   "oauthScopes": ["https://www.googleapis.com/auth/gmail.compose"],
  "runtimeVersion": "V8",
  "addOns": {
    "common": {
      "name": "Review Published Email Template",
      "logoUrl": "https://goodbookreviews.page/Logo.png",
      "useLocaleFromApp": true,
      "universalActions": [{
        "label": "Book Review ",
        "openLink": "https://www.goodbookreviews.page"
      }]
    },
    "gmail": {
      "contextualTriggers": [{
        "unconditional": {
        },
        "onTriggerFunction": "onGmailMessage"
      }],
      "composeTrigger": {
        "selectActions": [{
          "text": "Use Template",
          "runFunction": "onGmailCompose"
        }],
        "draftAccess": "NONE"
      }
    }
  }
}

The scopes that I specify in the OAuth consent screen page are here:

email

profile

openid

https://www.googleapis.com/auth/gmail.compose

The Email, profile, and openid are added by default and they are mandatory

Here are the scopes that I specify in the configuration page of the GSM SDK.

https://www.googleapis.com/auth/userinfo.email

https://www.googleapis.com/auth/userinfo.profile

https://www.googleapis.com/auth/gmail.compose

Anonoymous
  • 57
  • 8
  • 1
    Hello there @Anonoymous! I couldn't reproduce your issue with this version of the code. If you suspect that this is a scope issue, it may be because of triggers. Could you please check the [authorization modes](https://developers.google.com/gsuite/add-ons/concepts/editor-auth-lifecycle#authorization_modes) to make sure that the code can access the scopes? Also, please share too if the called functions run. You can do that by writing logs at the start of each called function and in uncertain areas. – Jacques-Guzel Heron May 27 '20 at 13:58
  • I checked out the authorization modes and I don't understand it. I am new to Google Apps Script. I have added all the scopes I think It uses based on the descriptions given in the OAuth consent screen configuration. I have made sure that the scopes in the OAuth consent screen are the same as the ones in the code and in the gsm SDK configuration. I have added a console.log to the beginning of each function and I don't see those logs in the console. I am assuming that means that the called functions do not run. I have no triggers. – Anonoymous May 28 '20 at 13:57
  • If the code works on testing, and only has issues after being published, I strongly recommend you to contact [G Suite Support](https://support.google.com/a/answer/1047213) because it's clear that the code works per se. To discard any misstep on the [publishing process](https://developers.google.com/gsuite/add-ons/how-tos/editor-publish-overview), could you please repeat the act from scratch? If the issue persists, repeat the publishing procedure with a legitimate add-on like [Cats](https://developers.google.com/gsuite/add-ons/cats-quickstart). – Jacques-Guzel Heron May 29 '20 at 13:54
  • I have already contacted gsuite support and they told me that it was out of their scope of support. Then they told me to come here. I have already built and run cats. I will start from scratch. I still think that it might be a scope problem. I am not sure if the scopes I declare in the code are correct so can you tell me if they are correct? I declare the same scopes in the code that I do in the OAuth consent screen and the GSM SDK configuration page.Thanks. – Anonoymous May 29 '20 at 15:09
  • I don't recognize the scope `openid`, where did you take it from? The second scope looks good to me (you can get more Gmail scopes [here](https://developers.google.com/gmail/api/auth/scopes#gmail_scopes)). The third scope also looks correct; you can read more about it in the [docs](https://developers.google.com/gsuite/add-ons/concepts/gsuite-scopes). The fourth scope is no longer necessary as announced [here](https://developers.google.com/gsuite/add-ons/releases/2018-2019#november_12_2019); it'll be ignored. Please, report back your findings after the publishing process testing. – Jacques-Guzel Heron Jun 01 '20 at 14:46
  • open ID is automatically added in the OAuth consent screen configuration so I matched all the scopes in the code and the GSM SDK config. Also, can you verify that the scopes I specified are what are required for this addon to function? I will remove that fourth scope. Thanks. – Anonoymous Jun 01 '20 at 16:59
  • You are right about the `openid` scope. Please forgive me for my misunderstanding. [Here](https://developers.google.com/identity/protocols/oauth2/scopes) you can see a full list of scopes compatibles with OAuth 2.0 with descriptions about their functions. – Jacques-Guzel Heron Jun 02 '20 at 06:29
  • Thank you. Can you tell me if I need to add more scopes? This is my first GSM project, also my first Google Apps Script project. Thanks. – Anonoymous Jun 02 '20 at 13:09
  • I don't think that this is a scope issue, let's leave that for later. First let me know about the results of your tests with the Cats add-on. And please, update your question with more information about your setup so we all can study it better. – Jacques-Guzel Heron Jun 02 '20 at 14:39
  • Sorry, I took several days to respond. I got caught up in another project. I used the cats addon at the beginning. I actually modified the cats code to get this code. The reason that it was not showing up in Gmail was that it takes 24-48 hours for changes to propagate so I waited a few days. It did show up in Gmail but then I got what I know was a scope problem. It said that 2 of my scopes were invalid so I worked on it for a while and changed all the scopes. I have updated my post to show what scopes I specify now. The card says: Add-on error Something went wrong when executing the add-on. – Anonoymous Jun 06 '20 at 18:25
  • Thank you for sharing your findings. Before continuing, could you please share the results of the log to better help you with that error? – Jacques-Guzel Heron Jun 08 '20 at 14:58
  • The console doesn't show anything irregular when I open it. It does not show my message though. – Anonoymous Jun 08 '20 at 17:01

1 Answers1

0

You are very close to making this add-on works. You only need to add some scopes to your manifest file. Your final manifest should look similar to this one:

{
  "timeZone":"America/Chicago",
  "dependencies":{

  },
  "exceptionLogging":"STACKDRIVER",
  "oauthScopes":[
    "https://www.googleapis.com/auth/gmail.compose",
    "https://www.googleapis.com/auth/gmail.addons.current.action.compose",
    "https://www.googleapis.com/auth/gmail.addons.execute",
    "https://www.googleapis.com/auth/script.locale"
  ],
  "runtimeVersion":"V8",
  "addOns":{
    "common":{
      "name":"Review Published Email Template",
      "logoUrl":"https://goodbookreviews.page/Logo.png",
      "useLocaleFromApp":true,
      "universalActions":[
        {
          "label":"Book Review ",
          "openLink":"https://www.goodbookreviews.page"
        }
      ]
    },
    "gmail":{
      "contextualTriggers":[
        {
          "unconditional":{

          },
          "onTriggerFunction":"onGmailMessage"
        }
      ],
      "composeTrigger":{
        "selectActions":[
          {
            "text":"Use Template",
            "runFunction":"onGmailCompose"
          }
        ],
        "draftAccess":"NONE"
      }
    }
  }
}

The rest of your code is correct. I deployed your add-on with this manifest, and it worked. Don't hesitate to ask any additional doubt if you need further clarification.

Jacques-Guzel Heron
  • 2,480
  • 1
  • 7
  • 16
  • Thank you so much! That worked exactly right! Is it possible to specify a subject line in the code? What about a recipient? Thanks. – Anonoymous Jun 09 '20 at 13:15