【问题标题】:400 Bad Request when attempting to insert QR code image for Google Authenticator尝试为 Google Authenticator 插入 QR 码图像时出现 400 Bad Request
【发布时间】:2013-06-08 21:53:05
【问题描述】:

我正在尝试在我的网站上使用 Google Authenticator 设置双重身份验证。我能够生成工作代码,但是当我将图像 URL 插入页面时,我在 Chrome 检查器中收到以下错误:

GET https://www.google.com/chart?chs=200x200&chld=M|0&cht=qr&chl=otpauth://totp/MyLabel?secret=THESECRET 400 (Bad Request)

生成二维码的代码:

try
  key = crypto.randomBytes(10).toString('hex')
catch error
  console.log "error generating code: #{error}"
encoded = base32.encode(key)
label = encodeURIComponent "MyLabel"
uri = "otpauth://totp/#{label}?secret=#{encoded}"
url = "https://www.google.com/chart?chs=200x200&chld=M|0&cht=qr&chl=#{uri}"

插入图片的客户端jQuery:

img = $("<img>").attr("src", url)
$("#qr_box").html("")
$("#qr_box").append(img)

这会在页面上产生以下 HTML:

<img src="https://www.google.com/chart?chs=200x200&amp;chld=M|0&amp;cht=qr&amp;chl=otpauth://totp/MyLabel?secret=THESECRET">

图片可以毫无问题地在新标签页中打开。图像仅在我的页面中成功显示大约 1/10 的时间;其他时候 Chrome 给出 400。我在这里遗漏了一些明显的东西吗?

【问题讨论】:

  • 浏览器向 Google 发出的请求似乎是以 text/html 而不是 image/png 类型返回的。我设置了一个简单的端点来请求 url,捕获响应,并将类型设置为 image/png,然后再返回到我的前端。如果有人知道一种更优雅的方法,我很想听听。

标签: node.js authentication coffeescript google-authentication


【解决方案1】:

对我来说,将 https://www.google.com 更改为 https://chart.googleapis.com 很有效。

【讨论】:

    猜你喜欢
    • 2022-12-14
    • 1970-01-01
    • 2018-06-20
    • 2013-02-16
    • 2015-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多