Не удается подключиться к Gmail API с помощью React - PullRequest
0 голосов
/ 18 января 2020

Я пытаюсь подключиться к API gmail в своем приложении реакции и отображать сообщения с указанием c Тема

У меня настроена аутентификация, но после просмотра всех документов и ресурсов я смог чтобы найти в Интернете, я не могу получить данные в моем приложении React.

Вот мой код для аутентификации, я просто заблудился о том, как даже начать получать данные Gmail, ссылки на документы немного сбивают с толку

import React, { Component } from 'react'
import { gapi } from 'gapi-script';
import './DataBackup.css'

export default class DataBackup extends Component {
constructor(props) {
    super(props);
    this.state = {
        isSignedIn: false,
        userId: 'me'
    }

}

componentDidMount() {

    const successCallback = this.onSuccess.bind(this);

    window.gapi.load('auth2', () => {
        this.auth2 = gapi.auth2.init({
            client_id: 'MYCLIENTID.apps.googleusercontent.com',
        })


        this.auth2.then(() => {
            console.log('Signing in');
            this.setState({
                isSignedIn: this.auth2.isSignedIn.get(),
            });
        });
    });

    window.gapi.load('signin2', function () {
        // Method 3: render a sign in button
        // using this method will show Signed In if the user is already signed in
        var opts = {
            width: 200,
            height: 50,
            client_id: 'MYCLIENTID.apps.googleusercontent.com',
            onsuccess: successCallback
        }
        gapi.signin2.render('loginButton', opts)
    })
}

onSuccess() {
    console.log("Succesfully Signed In")
    this.setState({
        isSignedIn: true,
        err: null
    })
}

onLoginFailed(err) {
    console.log("Error Signing In")
    this.setState({
        isSignedIn: false,
        error: err,
    })
}

//conditional function that if user signed in already it will display that they are
//if user not signed in then it will display the login button 
getContent() {
    if (this.state.isSignedIn) {
        return <p>hello user, you're signed in </p>
    } else {
        return (
            <div>
                <p>You are not signed in. Click here to sign in.</p>
                <button id="loginButton">Login with Google</button>
            </div>
        )
    }

}

render() {
    return (
        <div className="App">

            <h2>Sample App.</h2>

            {this.getContent()}

        </div>
    );
  }
}

С этим я могу нажмите кнопку входа в систему, и если авторизация прошла успешно, то будет console.log, что она прошла успешно

...