我正在GET请求的请求标头中向我的Google App Engine Go服务添加Firebase JWT。这是JavaScript:
const response = await fetch(
'https://some-app.appspot.com/_ah/data', {
method: 'get',
headers: {
'Authorization': 'Bearer ' + await Component.fetchJWT()
}
});
在端点上托管的我的Go服务上,收到请求。但是,会抛出错误:
error verifying ID token: illegal base64 data at input byte 0
以下是我处理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
}
}
令牌功能如下:
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\n", err)
}
token, err := client.VerifyIDToken(idToken)
if err != nil {
log.Fatalf("error verifying ID token: %v\n", err) // Error thrown here
}
log.Printf("Verified ID token: %v\n", token)
// [END verify_id_token]
return token
}
打印出令牌时,我觉得很好。但是,我从请求标头中检索它的方法是错误的吗?它可能看起来如此?
答案 0 :(得分:4)
删除授权标题拆分中的前导空格
<div>