I used Google.Apis.Firestore.v1 library to create a cloud firestore for my firebase project as follows, but when running my code I am getting an error related to the database naming I guess, and it has a regular expression format as shown in the error below, and how can I fix this issue?
using Google.Apis.Firestore.v1;
private static FirestoreService _firestoreService;
public static void IntializeFirestoreAdmin() {
GoogleCredential credential = GoogleCredential.GetApplicationDefault();
if (CloudManager.Credential.IsCreateScopedRequired)
{
credential = CloudManager.Credential.CreateScoped(FirestoreService.Scope.CloudPlatform);
}
_firestoreService = new FirestoreService(
new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = CloudManager.ApplicationName
});
}
public static void AddCloudFirestore() {
IntializeFirestoreAdmin();
var mydata = new GoogleFirestoreAdminV1Database {
LocationId = "nam5",
Type = "FIRESTORE_NATIVE",
Name = "projects/" + CloudManager.ProjectId + "/databases/(default)",
};
_firestoreService.Projects.Databases.Create(mydata, "projects/" + CloudManager.ProjectId).Execute();
}
Error :
Unhandled exception. The service firestore has thrown an exception.
HttpStatusCode is BadRequest.
Google.Apis.Requests.RequestError
database_id should be 4-63 characters, and valid characters are /[a-z][0-9]-/ [400]
Errors [
Message[database_id should be 4-63 characters, and valid characters are /[a-z][0-9]-/] Location[ - ] Reason[badRequest] Domain[global]
]
Google.GoogleApiException: The service firestore has thrown an exception. HttpStatusCode is BadRequest. database_id should be 4-63 characters, and valid characters are /[a-z][0-9]-/
at Google.Apis.Requests.ClientServiceRequest`1.ParseResponse(HttpResponseMessage response)
at Google.Apis.Requests.ClientServiceRequest`1.Execute()
2
Answers
The issue was that I didn't add the databaseId as a query parameter. For more details go to this link https://groups.google.com/g/google-cloud-firestore-discuss/c/EQ-04MnPyLk
If you go through the given method :
FirestoreService().Projects.Databases.Create
it hasparent
as Parameter anddatabaseId
as a Query parameter there is also a try this method section where you can verify your parameters are correct like for example :