gapi.auth2.getAuthInstance(). currentuser .get().grant()的结果是



任何想法是什么是在谷歌gapi javascript库调用方法gapi.auth2.getAuthInstance().currentUser.get().grant()的结果?

我正在尝试使用Google gapi实现增量授权。auth2库,但挣扎于GoogleUser.grant(options)实际返回的方法。

文档参考signIn方法,该方法返回Promise<GoogleUser>。https://developers.google.com/identity/sign-in/web/reference googleusergrantoptions

试了试,但结果似乎很不一样…

没有结果。grant()方法在某种程度上是signIn()方法的别名。不同之处在于,它被设计为请求额外的作用域,正如文档所解释的那样。当将popup值用于ux_mode选项时,这是完美的。

比如说,一个用户已经可以访问你的应用程序的基本范围,但随后有一个按钮,将允许用户与驱动器进行交互。当单击该按钮时,会发生如下操作:

const user = googleAuth.currentUser.get();
const scopes = user.getGrantedScopes().split(" ");
if(!scopes.includes("https://www.googleapis.com/auth/drive")){
user.grant({
scope: "https://www.googleapis.com/auth/drive",
}).then(()=>{
console.log(user.getGrantedScopes());
//do whatever you need to do in drive
});
}

我希望你明白基本的意思。我不太擅长解释,但希望这能有所帮助。

相关内容

最新更新