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

去解析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 ")
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料