Аутентификация с помощью googleapis не получает информацию о пользователе - PullRequest
0 голосов
/ 21 февраля 2020

Хорошо, поэтому я хочу аутентифицировать пользователя google, когда он / она желает это сделать.

Прямо сейчас я смог получить токен, когда пользователь входит в систему, используя следующие функции:

function createConnection() {
  return new google.auth.OAuth2(
    googleConfig.clientId,
    googleConfig.clientSecret,
    googleConfig.redirect
  );
}

function getConnectionUrl(auth) {
  const defaultScope = [
    'https://www.googleapis.com/auth/plus.me',
    'https://www.googleapis.com/auth/userinfo.email'
  ];
  return auth.generateAuthUrl({
    access_type: 'offline',
    prompt: 'consent', // access type and approval prompt will force a new refresh token to be made each time signs in
    scope: defaultScope
  });
}

function googleUrl() {
  const auth = createConnection();
  const url = getConnectionUrl(auth);
  return url;
}

Теперь я хочу получить пользовательские данные, используя возвращенный URL-адрес. Когда пользователь нажимает на ссылку:

const getGoogleAccountFromCode = async code => {
  const auth = createConnection();
  // get the auth "tokens" from the request
  const { tokens } = await auth.getToken(code);
  auth.setCredentials(tokens);
  // connect to google plus - need this to get the user's email
  const plus = getGooglePlusApi(auth);
  const me = await plus.people.get({
    resourceName: 'people/me',
    personFields: 'emailAddresses'
  });
  // get the google id and email
  const userGoogleId = me.data.id;
  const userGoogleEmail =
    me.data.emails && me.data.emails.length && me.data.emails[0].value;

  // return so we can login or sign up the user
  const isValidEmail = validateWhiteList(userGoogleEmail);
  if (isValidEmail) {
    return {
      id: userGoogleId,
      email: userGoogleEmail,
      tokens: tokens // you can save these to the user if you ever want to get their details without making them log in again
    };
  } else return { error: 'access denied' };
};

function getGooglePlusApi(auth) {
  return googleApi.google.people({ version: 'v1', auth });
}

Однако, я получаю эту ошибку при вызове plus.people.get:

'The caller does not have permission to request "people/me". Request requires one of the following scopes: [profile].',

Кто-нибудь знает, что я должен сделать, чтобы совершить sh это?

Я действительно ценю это

...