Javascript Google使用React app.JS登录,承诺不初始化客户端id

Javascript Google使用React app.JS登录,承诺不初始化客户端id,javascript,reactjs,promise,react-redux,google-oauth,Javascript,Reactjs,Promise,React Redux,Google Oauth,我在尝试在我的web应用程序中实现Google登录时遇到问题。 它是一个React应用程序,后端有一个RESTAPI Python烧瓶 使用google API plataform库创建gapi对象(根据), 假设我这样做: <script src="https://apis.google.com/js/platform.js?onload=init" async defer></script> 为了完成上述要求,我正在这样做 在子组件ClientOAuth.hmtl中

我在尝试在我的web应用程序中实现Google登录时遇到问题。 它是一个React应用程序,后端有一个RESTAPI Python烧瓶 使用google API plataform库创建gapi对象(根据), 假设我这样做:

<script src="https://apis.google.com/js/platform.js?onload=init" async defer></script>
为了完成上述要求,我正在这样做

在子组件ClientOAuth.hmtl中,我创建了一个承诺,如下所示:

static getGoogleAPI=()=>{

    if (!this.googleAPIPromise){
        //Loading the Google API into an scrip element in the page
        this.googleAPIPromise=new Promise((resolve, reject)=>{
            window.start=()=>{
                // resolve the promise 
                // When this script is loaded a var called gapi=window.gapi is made available
                resolve(window.gapi)

                //Tidy up
                delete window.start;
              };
            const script = document.createElement("script");
            script.src = `https://apis.google.com/js/platform.js?onload=start`;
            script.async = true;
            script.onerror=()=>reject();
            document.body.appendChild(script);

        });
    }
    //We return the just created promise
    return this.googleAPIPromise;
}
后来我把这个承诺称为componentDidMount

componentDidMount(){
    this.getGoogleAPI().then((gapi)=>{
        gapi.load('auth2',( ()=>{
            console.log("trying to init the client_id in start") 
            window.oauth2 = window.gapi.auth2.init({
                client_id: '11111111111-aeimkno2v0lcs5u1d6fdk1o0uvtojpt0.apps.googleusercontent.com'
                // Scopes to request in addition to 'profile' and 'email'
                // scope: 'additional_scope'
            });
        }))
    })
但是我得到了这个错误:

uO {message: "Missing required parameter 'client_id'", stack: "gapi.auth2.ExternallyVisibleError: Missing require…h-C23aWePw0rPNrzGe8eu-Ej8ZQ/cb=gapi.loaded_0:1:15"}

有人能帮我吗?

在我按下面的方式更改代码后,它会发出警告。我将代码移动到componentWillMount

componentWillMount(){
   console.log("Inside componentWillMount")

   this.getGoogleAPI().then((gapi)=>{
        gapi.load('auth2',( ()=>{
            console.log("trying to init the client_id in start") 
            window.auth2 = window.gapi.auth2.init({
                client_id: '11111111111-aeimkno2v0lcs5u1d6fdk1o0uvtojpt0.apps.googleusercontent.com'
                // Scopes to request in addition to 'profile' and 'email'
                // scope: 'additional_scope'
            }).then( ()=>{
                window.auth2 = gapi.auth2.getAuthInstance();
                // console.log(window.auth2.isSignedIn.get()); 
                this.setState({ isGoogleScriptLoaded: true})       
                });
        }))
    })
所以需要.then(()=>{})。 现在React应用程序正在运行

我仍然在React中遇到一个错误,这应该是因为我没有在React生命周期的正确时间初始化客户端id

这是ReactJS中的错误:

message
:
"Missing required parameter 'client_id'"
stack
:
"gapi.auth2.ExternallyVisibleError: Missing required parameter 'client_id
我不会这样离开代码。我希望通过Redux更好地管理应用程序状态可以解决我的问题

Python后端错误和解决方案:

对于那些在后端遇到这些错误的人,我在这里看到一些人正在寻找对此错误的响应:

invalid_grant 
Malformed auth code.
请注意,短命的代码需要发送到Google OAuth2,而不带“”。 我必须先删除它,然后再将其发送到谷歌

因此,如果您发送到谷歌: “4/ERD456YUIP56PXAOTTZSNCKYOCSXO1Ce1FGLOHJSCV07ZB22NUONTP0MPGV0KXNQEOTISLNIKYIST9PY8”

谷歌Oauth2将投诉并拒绝您的请求。 您需要发送

4/ERD456YUIP56PXAOTTZNSCXKYOCSXO1Ce1FGLOHJSCV07ZB22NUONTP0MPGV0KXNQEOTISLNIKYI0KST9PY8

没有“

希望能有帮助

invalid_grant 
Malformed auth code.