我有一小段 javascript 代码,它一直在 angular 应用程序中工作:
async googleSignIn(redirectUrl, userId) {
let clientID=encodeURIComponent('redacted');
let redirectURI=encodeURIComponent(redirectUrl);
let scope=encodeURIComponent('https://www.googleapis.com/auth/calendar')+"+"+encodeURIComponent('https://www.googleapis.com/auth/calendar.events')
let url =
`https://accounts.google.com/o/oauth2/v2/auth?response_type=code&
client_id=${clientID}&
redirect_uri=${redirectURI}&
access_type=offline&
prompt=consent&
scope=${scope}&
state=${userId}`;
window.open(url,"_self");
}
什么都没有改变,但现在它不再起作用了,相反,我收到了一个缺少参数的 400 错误。每次加载链接时,缺少哪一个都会发生变化(clientId、scope、redirectURI——它总是说缺少其中一个,即使它们存在)。
我通过使用调试器并检查变量来验证一切都在那里。我什至复制了变量的内容,将其粘贴到一个新选项卡中,然后就成功了。我真的很难过,请有人帮我看看我在这里做错了什么吗?
答案 0 :(得分:0)
Template literals 将在您编写字符串时呈现字符串,包括 newline
、spaces
、...这些换行符是问题所在。
删除多余的字符将解决问题:
let url = `https://accounts.google.com/o/oauth2/v2/auth?response_type=code&client_id=${clientID}&redirect_uri=${redirectURI}&access_type=offline&prompt=consent&scope=${scope}&state=${userId}`;