0

Hi now i am using firebase REST API

When i used public database rule

it's successful to set,update or delete data to database

/* database rule */
{
  "rules": {
    ".read": true,
    ".write": true
  }
}

/* Put data to database successful */
curl -X PUT -d '{
  "alanisawesome": {
    "name": "Alan Turing",
    "birthday": "June 23, 1912"
  }
}' 'https://mydatabe.firebaseio.com/users.json'

But i wanted to use auth REST, how can i get token? Here is my step.

First change database rule to

{
  "rules": {
    ".read": "auth != null",
    ".write": "auth != null"
  }
}

Then i used Firebase SDK to login by google and get token

You can see i print token by "console.log(token);"

For example i got token is "AABBCCDD"

var provider = new firebase.auth.GoogleAuthProvider(); 
firebase.auth().signInWithPopup(provider).then(function(result) {      
  var token         = result.credential.accessToken;      
  var user          = result.user;   
  console.log(token);
}).catch(function(error) {
  var errorCode     = error.code;
  var errorMessage  = error.message;     
  var email         = error.email;   
  var credential    = error.credential;      
});

I checked my Firebase console > Auth > User and there have my account

So that mean i successful to register my account to database

Second i used REST API like

curl -X PUT -d '{
  "alanisawesome": {
    "name": "Alan Turing",
    "birthday": "June 23, 1912"
  }
}' 'https://mydatabe.firebaseio.com/users.json?auth=AABBCCDD'

But it returned { "error" : "Could not parse auth token." }

So what is the problem? which step is worng?

Thanks

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
CK vir
  • 287
  • 4
  • 17

1 Answers1

0

I don't know what exactly is wrong... but to debug you could try

  1. first only read via REST API
  2. check if the token is valid via https://jwt.io/. The token needs to have three parts separated with a dot, maybe you just didn't copy the whole string.
Tom
  • 564
  • 1
  • 3
  • 8