NodeJS - URI пользовательской схемы недопустимы для типа клиента «WEB» - PullRequest
2 голосов
/ 17 марта 2020

Я пытаюсь использовать Oauth2 с googleapis, но у меня появляется такая ошибка. Я обнаружил, что кто-то получил ту же ошибку, но они используют IOS, я использую NodeJS в этом случае.

Я следую этому примеру: https://dev.to/uddeshjain/authentication-with-google-in-nodejs-1op5

Это мой код:

const { google } = require('googleapis');
const express = require('express')
const OAuth2Data = require('./google_key.json')

const app = express()

const CLIENT_ID = OAuth2Data.client.id;
const CLIENT_SECRET = OAuth2Data.client.secret;
const REDIRECT_URL = OAuth2Data.client.redirect

const oAuth2Client = new google.auth.OAuth2(CLIENT_ID, CLIENT_SECRET, REDIRECT_URL)
var authed = false;

app.get('/', (req, res) => {
    if (!authed) {
        const url = oAuth2Client.generateAuthUrl({
            access_type: 'offline',
            scope: 'https://www.googleapis.com/auth/gmail.readonly'
        });

        res.redirect(url);
    } else {
        const gmail = google.gmail({ version: 'v1', auth: oAuth2Client });
        gmail.users.labels.list({
            userId: 'me',
        }, (err, res) => {
            if (err) return console.log('The API returned an error: ' + err);
            const labels = res.data.labels;
            if (labels.length) {
                console.log('Labels:');
                labels.forEach((label) => {

                });
            } else {

            }
        });
        res.send('Logged in')
    }
})

app.get('/auth/google/callback', function (req, res) {
    const code = req.query.code
    if (code) {
        oAuth2Client.getToken(code, function (err, tokens) {
            if (err) {
                console.log('Error authenticating')
                console.log(err);
            } else {
                console.log('Successfully authenticated');
                oAuth2Client.setCredentials(tokens);
                authed = true;
                res.redirect('/')
            }
        });
    }
});

const port = process.env.port || 3000
app.listen(port, () => console.log(`Server running at ${port}`));

И файл учетных данных: google_key.json

{
    "client": {
        "id": "xxx-rm7s51vmr320brk5b5mrhopgma0e65o9.apps.googleusercontent.com",
        "secret": "nIMQpHo6LRMs128Hp8sjh6-bxxxx",
        "redirect": "testauth.com:3000"
    },
    "credentials": {
        "access_token": "your access_token",
        "token_type": "Bearer",
        "expires_in": 3600,
        "refresh_token": "your refresh_token"
    }
}

Любая помощь!

...