为什么我突然无法在AS3 AIR Android项目中使用Firebase REST API刷新idToken。?

时间:2016-12-15 22:53:15

标签: android actionscript-3 firebase firebase-authentication firebase-storage

我已使用this指南集成了Firebase身份验证和存储,但它运行正常。昨天突然,我无法刷新令牌。每次调用refreshToken(idToken)方法时,都会出现400:Invalid Id Token错误。

我没有更改代码。有一天我正在测试应用程序并且工作正常,第二天我检查了同一个应用程序并且它没有工作。我不知道该怎么做。非常感谢任何帮助。

以下是我用来刷新令牌的代码:

    private function refreshToken(idToken:String):void
    {
        trace("refreshing id token");
        var header:URLRequestHeader = new URLRequestHeader("Content-Type", "application/json");

        var myObject:Object = new Object();
        myObject.grant_type = "authorization_code";
        myObject.code = idToken;

        var request:URLRequest = new URLRequest("https://securetoken.googleapis.com/v1/token?key="+FIREBASE_API_KEY);
        request.method = URLRequestMethod.POST;
        request.data = JSON.stringify(myObject);
        request.requestHeaders.push(header);

        var loader:URLLoader = new URLLoader();
        loader.addEventListener(flash.events.Event.COMPLETE, refreshTokenLoaded);
        loader.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
        loader.load(request);   
    }

1 个答案:

答案 0 :(得分:1)

Firebase最近改变了获取access_token的方式。

我已更新指南以反映更改,您现在需要在登录时添加额外参数。

private function login(email:String, password:String):void
{
    var myObject:Object = new Object();
    myObject.email = email;
    myObject.password = password;
    myObject.returnSecureToken = true; <-- New parameter

    var header:URLRequestHeader = new URLRequestHeader("Content-Type", "application/json");

    var request:URLRequest = new URLRequest("https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyPassword?key="+FIREBASE_API_KEY);
    request.method = URLRequestMethod.POST;
    request.data = JSON.stringify(myObject);
    request.requestHeaders.push(header);

    var loader:URLLoader = new URLLoader(); 
    loader.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
    loader.addEventListener(flash.events.Event.COMPLETE, signInComplete);
    loader.load(request);               
}

在回复中,您现在将获得一个refreshToken,您必须使用以下函数将其替换为访问令牌:

private function refreshToken(refreshToken:String):void
{
    var header:URLRequestHeader = new URLRequestHeader("Content-Type", "application/json");

    var myObject:Object = new Object();
    myObject.grant_type = "refresh_token";
    myObject.refresh_token = refreshToken;          

    var request:URLRequest = new URLRequest("https://securetoken.googleapis.com/v1/token?key="+FIREBASE_API_KEY);
    request.method = URLRequestMethod.POST;
    request.data = JSON.stringify(myObject);
    request.requestHeaders.push(header);

    var loader:URLLoader = new URLLoader();
    loader.addEventListener(flash.events.Event.COMPLETE, refreshTokenLoaded);
    loader.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
    loader.load(request);   
}