Kazavo
Kazavo

Reputation: 71

Enable Firebase Authentication via API or SDK

As part of my project, I need to enable the firebase authentication from API or SDK. I didn't found any solution - the only way is to enable it from the console.

Is anyone familiar with that?

Enable Autentication

EDIT: I finally found the solution for enabling GCP service. The following link explains how to enable GCP services via REST: https://cloud.google.com/service-usage/docs/enable-disable

Upvotes: 4

Views: 334

Answers (2)

Kazavo
Kazavo

Reputation: 71

After my long search I found the way: The serviceusage Google API provides a REST API for enabling/disabling GCP service. PLease note that after the service enabled, it needed to enable the signIn method. You can see examples from my code below.

Service Enabling:

 url = 
'https://serviceusage.googleapis.com/v1/projects/{IDENTIFIER:}/services:batchEnable'.format(IDENTIFIER=project_idetifier)
  if verbose: print(bcolors.INFO + 'GET URL: {url:}'.format(url=url) + bcolors.ENDC)
  headers={'Authorization': 'Bearer {token:}'.format(token=access_token),\
    'Accept': 'application/json', 'Content-Type': 'application/json'}
  payload = {
    "serviceIds": ["identitytoolkit"],
  }
  r = httpReq.post(url, data=json.dumps(payload), headers=headers)
  resp_body = json.loads(r.text)

Enable SignIn Method:

  url = 'https://identitytoolkit.googleapis.com/admin/v2/projects/{project_id:}/config?updateMask=signIn.email.enabled,signIn.email.passwordRequired'.format(project_id=project_id)
  headers={'Authorization': 'Bearer {token:}'.format(token=access_token),\
    'Accept': 'application/json', 'Content-Type': 'application/json'}
  payload = {"signIn": {"email": {"enabled": True, "passwordRequired": True}}}
  r = httpReq.patch(url, data=json.dumps(payload), headers=headers)
  resp_body = json.loads(r.text)

Just a note: The access token was generated from the service account JSON file:

  credentials = service_account.Credentials.from_service_account_file(
          <YOUR_JSON_SERVICE_ACCOUNT_PATH>, scopes=CREDENTIAL_SCOPES)
  credentials.refresh(requests.Request())
  return credentials.token

Upvotes: 2

Related Questions