title: Gmail API with React
body: <p>I try to create app with Gmail API and with React. For authorization I use <a href="https://www.npmjs.com/package/react-google-login" rel="nofollow noreferrer">lib</a> 
  react-google-login</p>
  
  <pre><code>   export default class App extends Component {
    service = new Service();
  
    render() {
      const responseGoogle = (response) =&gt; {
        this.service.getMessagesList();
      }
      return (
        &lt;div&gt;
            &lt;GoogleLogin
      clientId="YOUR_ID.apps.googleusercontent.com"
      buttonText="Login"
      onSuccess={responseGoogle}
  
    /&gt;
  </code></pre>
  
  <p>After authorization I send fetch request to gmail <a href="https://developers.google.com/gmail/api/v1/reference/users/messages/list" rel="nofollow noreferrer">API</a> and try to get my emails list. But server return me 401. Why? What I did wrong? </p>
  
  <p>My fetch:</p>
  
  <pre><code>export default class Service {
  _apiBase = "https://www.googleapis.com/gmail/v1/users/me";
  async getResource(url) {
    const res = await fetch(`${this._apiBase}${url}`);
    if (!res.ok) {
      throw new Error(`Could not fetch ${url}, received ${res.status}`);
    }
    const body = await res.json();
    return body;
  }
  
  getMessagesList = async () =&gt; {
    const list = await this.getResource(`/messages`);
    return list;
  };
  </code></pre>