尝试为Google身份验证器插入QR代码图像时出现400错误请求

时间:2013-06-12 17:11:32

标签: node.js authentication coffeescript google-authentication

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

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

生成QR码的代码:

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.我在这里错过了一些明显的东西吗?

1 个答案:

答案 0 :(得分:11)

对我来说,它可以将 https://www.google.com 更改为 https://chart.googleapis.com