【问题标题】:Google authentication request URL breaking (Clientside auth)Google 身份验证请求 URL 中断(客户端身份验证)
【发布时间】:2021-01-06 17:37:38
【问题描述】:

我有一小段一直在 Angular 应用程序中工作的 javascript 代码:

  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、范围、redirectURI - 它总是说其中一个丢失,即使它们存在)。

我通过使用调试器并检查变量来验证一切都在那里。我什至复制了变量的内容,将其粘贴到一个新选项卡中,然后它就可以工作了。我真的很难过,请有人帮我看看我在这里做错了什么?

【问题讨论】:

    标签: javascript client-side google-authentication


    【解决方案1】:

    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}`;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-23
      • 2016-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-04
      • 1970-01-01
      • 2011-04-09
      相关资源
      最近更新 更多