简体   繁体   中英

Gmail API permission to send emails on user consent in my react-redux application?

I am working on a react-redux application where the user can submit their feedback/complaint to respective organization. The idea is to send emails on user's consent for which I am using Gmail API. I have integrated a feedback form.

Now the user has to be logged in before submission of the message. I do not want to add Gmail send ( https://www.googleapis.com/auth/gmail.send ) scope with login. Login(on the header) only works with the profile and read-only scopes. Send email permission should only be asked separately when submit is clicked and then take further action on that consent. How can I achieve this send email scope separately? I am writing down my login code as I am not sure what has to be done on submit.

var clientId = 'xxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com';                    
var API_KEY = 'xxxxxxxxxxxxxxxxxxxxxxxx';
var scopes = 'https://www.googleapis.com/auth/gmail.readonly '+'https://www.googleapis.com/auth/userinfo.profile';

var DISCOVERY_DOCS = ["https://www.googleapis.com/discovery/v1/apis/gmail/v1/rest"];

componentDidMount() {
  this.handleClientLoad();
}


 handleClientLoad = () => {
    const script = document.createElement("script");
    script.src = "https://apis.google.com/js/client.js";

  script.async = true
  script.onload = () => {
    window.gapi.load('client:auth2', () => {
      window.gapi.client.setApiKey(API_KEY);
      window.setTimeout(this.initClient(), 1);
    });
  }
  document.body.appendChild(script)
}

initClient = () => {
window.gapi.client.init({
  apiKey: API_KEY,
  discoveryDocs: DISCOVERY_DOCS,
  clientId: clientId,
  scope: scopes,
}).then(() => {
window.gapi.auth2.getAuthInstance().isSignedIn.listen(this.updateSigninStatus());
      this.updateSigninStatus(window.gapi.auth2.getAuthInstance().isSignedIn.get());
    });
  }

updateSigninStatus = (isSignedIn) => {
if (isSignedIn) {
  window.gapi.auth2.getAuthInstance().currentUser.listen((user) => {
    console.log(user);
  });
} else {
    console.log("error");
  }
 }

authClick = () => {
    window.gapi.auth2.getAuthInstance().signIn().then((user) =>
       console.log(user)
 )}

Following is the order:

  1. User logs in
  2. Fill out the feedback form
  3. Hit submit (ask user's consent using Gmail send scope)
  4. Perform actions on the basis of consent.

I am stuck on the 3rd step. How to add scope separately to get user consent?

you cant your going to need to request consent at the time you authenticate your user. You cant split that up.

You will need to add the Gmail.send scope here.

var scopes = 'https://www.googleapis.com/auth/gmail.readonly '+'https://www.googleapis.com/auth/userinfo.profile';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM