好的,我想在用户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].',
有人知道我应该怎么做吗?
我真的很感激