javascript使用gmail API(auth)发送邮件,错误在哪里

时间:2018-10-12 04:25:03

标签: javascript reactjs google-api gmail-api google-oauth2

此代码无效。我使用ReactJS而不是普通的js来加载脚本,这部分工作正常(我获得了授权),但是随后“ url”却以某种方式被误认为

*我要做的是通过使用gmail API,搜索google和API文档来发送邮件,到目前为止没有运气

componentDidMount() {
    this.handleSort('stocks_present', 'total_invested_EUR')();

    const script = document.createElement("script");
    script.src = "https://apis.google.com/js/client.js";
    document.body.appendChild(script);
    script.onload = () => this.sendMail(script);
}

wait = ms => new Promise((r, j) => setTimeout(r, ms));

sendMail = async (script) => {
    let clientId = 'xxxxxxxx.apps.googleusercontent.com';
    let apiKey = 'xxxxxxxxxxxxxxxxxxx5ZDsC3l9TY';
    let scopes = 'https://www.googleapis.com/auth/gmail.send';

    const google_auth = async () => {
        window.gapi.client.setApiKey(apiKey);
        await this.wait(1000);

        console.log('starting -> authorized')
        window.gapi.auth2.authorize({
            client_id: clientId,
            scope: scopes,
            immediate: true
        }, () => {
            console.log('loading gmail');
            window.gapi.client.load('gmail',
                {
                    callback: () => {
                        console.log('loaded'); // it doesn't get here
                        let sendRequest = window.gapi.client.gmail.users.messages.send({
                            'userId': 'me',
                            'resource': {
                                'raw': window.btoa(email).replace(/\+/g, '-').replace(/\//g, '_')
                            }
                        });
                        sendRequest.execute();
                    },
                    onerror: (e) => console.log('error'),
                    timeout: 1000,
                    ontimeout: (e) => console.log('error')
                });
        });
        return false;
    }

    let counter = 10;
    while (counter > 1) {
        if (script.getAttribute('gapi_processed')) {
            console.log('auth gained');
            google_auth();
            break;
        } else {
            await this.wait(1000);
            counter--;
            console.log('waiting for gapi ' + counter);
        }
    }
}

我明白了

waiting for gapi 9
auth gained
starting -> authorized
loading gmail

然后是错误

GET https://content.googleapis.com/discovery/v1/apis/gmail/%5Bobject%20Object%5D/rest?fields=kind%2Cname%2Cversion%2CrootUrl%2CservicePath%2Cresources%2Cparameters%2Cmethods%2CbatchPath%2Cid&pp=0&key=xxxxxxxxxxxxxxx3l9TY 404

如果我手动进行操作,我会得到

{"error":{"errors":[{"domain":"global","reason":"notFound","message":"Not Found"}],"code":404,"message":"Not Found"}}

更新:我可以使用此功能

            window.gapi.client.load('gmail', 'v1', () => {
                console.log('loaded');
                let sendRequest = window.gapi.client.gmail.users.messages.send({
                    'userId': 'me',
                    'resource': {
                        'raw': window.btoa(email).replace(/\+/g, '-').replace(/\//g, '_')
                    }
                });
                sendRequest.execute();
                console.log('message sent');
            });

但是从理论上讲,这种方式已被弃用,那么我如何使它起作用?

  

gapi.client.load(名称,版本,回调)。请加载API   与发现文件。将客户端库接口加载到   特定的API。如果未提供回调,则goog.Thenable为   回到。加载的API接口将采用以下形式   gapi.client.api.collection.method。例如,主持人API   将创建诸如gapi.client.moderator.series.list之类的方法。

0 个答案:

没有答案