0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

# Dart VM上でFirebase Storageにアクセスする

Posted at

こちらとほぼ同じですが、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);
  }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?