pythonfirebaserestfirebase-remote-config

Google Firebase Config APIs


I am trying to call PUT apis of google firebase but I am getting code: 400 error.

payload = {
  "parameters": [{
    "key": "key1",
    "defaultValue": [{
      "value": "123"
    }]
    }, {
    "key": "otherKey",
    "defaultValue": [{
      "value": "456"
    }]
  }]
}

response = authed_session.put(
  "https://firebaseremoteconfig.googleapis.com/v1/projects/test/remoteConfig",
  data = payload
)

enter image description here I have modified payload with different combination but it didnt work for me.


Solution

  • I noticed that the payload you’re sending may not be valid. Looking at the docs, the expected request body is an instance of RemoteConfig. So the payload would look like:

    payload = {
        "parameters": {
            "key1": {
              "defaultValue": {
                "value": "123"
              },
              "valueType": "STRING"
            },
            "key2": {
              "defaultValue": {
                "value": "456"
              },
              "valueType": "STRING"
            }
        }
    }
    

    Also, convert your payload into a JSON-formatted string then pass it to the data argument of authed_session.put. Note that I’m passing If-Match: * in the headers which forces an update:

    response = authed_session.put(
      "https://firebaseremoteconfig.googleapis.com/v1/projects/<PROJECT_ID>/remoteConfig",
      data=json.dumps(payload),
      headers={
        "If-Match": "*"
      }
    )
    

    Alternatively, you can just pass the payload into the json argument of authed_session.put:

    response = authed_session.put(
      "https://firebaseremoteconfig.googleapis.com/v1/projects/<PROJECT_ID>/remoteConfig",
      json=payload,
      headers={
        "If-Match": "*"
      }
    )
    

    Complete code would look like:

    from google.auth.transport.requests import AuthorizedSession
    from google.oauth2 import service_account
    
    credentials = service_account.Credentials.from_service_account_info(
      <SERVICE_ACCOUNT_OBJECT>,
      scopes=['https://www.googleapis.com/auth/firebase.remoteconfig',
              "https://www.googleapis.com/auth/cloud-platform"],
    )
    
    authed_session = AuthorizedSession(credentials=credentials)
    
    payload = {
        "parameters": {
            "key1": {
              "defaultValue": {
                "value": "123"
              },
              "valueType": "STRING"
            },
            "key2": {
              "defaultValue": {
                "value": "456"
              },
              "valueType": "STRING"
            }
        }
    }
    
    response = authed_session.put(
      "https://firebaseremoteconfig.googleapis.com/v1/projects/<PROJECT_ID>/remoteConfig",
      json=payload,
      headers={
        "If-Match": "*"
      }
    )
    
    print(response.text)