dsgdsf12312 2018-01-09 13:14
浏览 655
已采纳

去解析JWT:错误验证ID令牌:输入字节0处的非法base64数据

I'm adding a Firebase JWT to my Google App Engine Go-service in the request header of a GET request. Here's the JavaScript:

const response = await fetch(
                'https://some-app.appspot.com/_ah/data', {
                    method: 'get',
                    headers: {
                        'Authorization': 'Bearer ' + await Component.fetchJWT()
                    }
                });

On my Go service, which is hosted at the endpoint, the request is received. However, the error is thrown:

error verifying ID token: illegal base64 data at input byte 0

Here is how I process the JWT:

func (ma *myapp) SomeHandler(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "application/json")
    reqToken := r.Header.Get("Authorization")
    splitToken := strings.Split(reqToken, "Bearer")
    reqToken = splitToken[1]
    fmt.Println(reqToken) // Prints the token correctly

    lib.VerifyIDToken(ma.fbapp, reqToken) // Error is thrown in this function

    enc := json.NewEncoder(w)
    err := enc.Encode(SomeData)

    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }
}

The token function is as such:

func VerifyIDToken(app *firebase.App, idToken string) *auth.Token {
    // [START verify_id_token]
    client, err := app.Auth(context.Background())
    if err != nil {
        log.Fatalf("error getting Auth client: %v
", err)
    }

    token, err := client.VerifyIDToken(idToken)
    if err != nil {
        log.Fatalf("error verifying ID token: %v
", err) // Error thrown here
    }

    log.Printf("Verified ID token: %v
", token)
    // [END verify_id_token]

    return token
}

When printing out the token, it appears fine to me. However, is my method of retrieving it from the request header wrong? It could appear so?

  • 写回答

1条回答 默认 最新

  • dosi8657 2018-01-09 13:21
    关注

    Remove the leading space in Authorization header split

    strings.Split(reqToken, "Bearer ")
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 QTableWidget重绘程序崩溃
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含
  • ¥15 麒麟V10桌面版SP1如何配置bonding