问题描述
我正在尝试从我的 Google 帐户中列出目录人员。
export class People {
private auth: Auth.oauth2client;
private initialized: boolean = false;
private accesstoken: string;
constructor(private readonly clientEmail: string,private readonly pKey: string) {}
public async people() {
await this.initialize();
const googlePeople = google.people({ version: 'v1',auth: this.auth });
const people = await googlePeople.people.listDirectoryPeople();
return people.data;
}
public async setToken(accesstoken: string) {
this.accesstoken = accesstoken;
}
private async initialize() {
if (this.initialized) {
return;
}
this.auth = new google.auth.OAuth2({
clientId: 'xxx',clientSecret: 'zzz',redirectUri: 'http://localhost:3000/people',});
this.auth.setCredentials({
access_token: this.accesstoken,scope: 'https://www.googleapis.com/auth/directory.readonly',});
this.initialized = true;
}
}
error: {
code: 403,message: "Request had insufficient authentication scopes.",errors: [
{
message: "Insufficient Permission",domain: "global",reason: "insufficientPermissions"
}
],status: "PERMISSION_DENIED"
}
我在 Google 文档中没有找到任何关于如何在 JS 中正确设置 People API 范围的信息。在这种情况下如何设置范围?
解决方法
“请求的身份验证范围不足。”
表示当前经过身份验证的用户,即您登录以创建访问令牌的用户。没有授予您足够的权限来运行您尝试运行的 reuqest。
您似乎正在尝试运行 people.listDirectoryPeople 方法,根据文档,该方法需要以下范围
因为您似乎包含以下范围。
范围:'https://www.googleapis.com/auth/directory.readonly',
您当前使用的访问令牌不是在该范围内创建的,您需要再次运行您的应用程序并强制它再次请求用户访问权限并请求该范围。
要么重置 cookie,要么让用户通过 Permissions 在其 Google 开发者帐户上强行删除应用程序访问权限。你如何做到这一点取决于你。但是您需要一个具有适当范围的新访问令牌才能使用该方法。
people.get me work
People.get 之所以有效,是因为它使用不同的范围集,以下任何一项都可以。
但这意味着您可以使用 https://www.googleapis.com/auth/userinfo.profile
并且 people.get
会起作用,但 people.listDirectoryPeople
不起作用,因为您没有足够的权限,您需要 https://www.googleapis.com/auth/directory.readonly
。>