ahsankhatri / firestore-php

Firestore PHP Client (without gRPC extension)
MIT License
62 stars 57 forks source link

Authentication with Service Account #20

Open universe-42 opened 4 years ago

universe-42 commented 4 years ago

According to official Google library its the best way how to work with resources (at least for beginners). I can try write PR, but please tell me your opinion for this. Thanks.

Do not use API keys for local or production applications. For almost all cases, you should use service accounts. (Authentication DOC)

superbigsoft commented 4 years ago

@universe-42 could you show me how to do Authentication with Service Account? Many thanks.

itsyourap commented 2 years ago

Create Kreait\Firebase\Factory() instance using your service account credentials and then create MrShan0\PHPFirestore\FirestoreClient('firebase-project-id', null). Yes, you can push null as your API Key. Then use setHttpClient($FACTORY->createApiClient()); to use the Service Account credentials with FirestoreClient.

Example:-

$FACTORY = (new \Kreait\Firebase\Factory)->withServiceAccount('service-account-path.json');
$FIRESTORE_CLIENT = new MrShan0\PHPFirestore\FirestoreClient('project-id', null);
$FIRESTORE_CLIENT->setHttpClient($FACTORY->createApiClient());
var_dump($FIRESTORE_CLIENT->listDocuments('/some-collection/')); //Works Perfectly
Saifallak commented 2 years ago

Create Kreait\Firebase\Factory() instance using your service account credentials and then create MrShan0\PHPFirestore\FirestoreClient('firebase-project-id', null). Yes, you can push null as your API Key. Then use setHttpClient($FACTORY->createApiClient()); to use the Service Account credentials with FirestoreClient.

Example:-

$FACTORY = (new \Kreait\Firebase\Factory)->withServiceAccount('service-account-path.json');
$FIRESTORE_CLIENT = new MrShan0\PHPFirestore\FirestoreClient('project-id', null);
$FIRESTORE_CLIENT->setHttpClient($FACTORY->createApiClient());
var_dump($FIRESTORE_CLIENT->listDocuments('/some-collection/')); //Works Perfectly

not working on lastest version