こちらとほぼ同じですが、scopesに'https://www.googleapis.com/auth/cloud-platform'
を追加しないと403エラーが帰ってきて詰まってたので備忘録です
void main() async {
var json = {};
var credential = await Credentials.fetch();
var fbClient = FirebaseClient(credential);
//データ.jsonに書き込み
await fbClient.post(
'https://firebasestorage.googleapis.com/v0/b/[ProjectID].appspot.com/o/データ.json',
json);
}
class Credentials {
static Future<String> fetch() async {
var pk = await getPrivateKey();
var accountCredentials = ServiceAccountCredentials.fromJson({
'private_key_id': pk['private_key_id'],
'private_key': pk['private_key'],
'client_email': pk['client_email'],
'client_id': pk['client_id'],
'type': 'service_account',
});
var scopes = [
'https://www.googleapis.com/auth/datastore',
'https://www.googleapis.com/auth/cloud-platform'//stackoverflowとかだとこれはなかったけど、ないと権限が得られず403エラーを吐いてしまう
];
var client = http.Client();
var credentials = await obtainAccessCredentialsViaServiceAccount(
accountCredentials, scopes, client);
client.close();
return credentials.accessToken.data;
}
static Future<Map<String, dynamic>> getPrivateKey() async {
String jsonString =
await File('/Path/to/firebase_private_key/keyfile.json')
.readAsString();
return json.decode(jsonString);
}
}