Skip to content
This repository was archived by the owner on Dec 30, 2024. It is now read-only.

Commit 53e88b4

Browse files
Add Admin SDK snippet -- getting service account credentials
Change-Id: I2e3bd92d5ca97fba1ad6af194dac26ac48b99967
1 parent 6a366dc commit 53e88b4

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

admin/build.gradle

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
group 'com.google.firebase.example'
2+
version '1.0'
3+
4+
apply plugin: 'java'
5+
apply plugin: 'application'
6+
7+
mainClassName = 'com.google.firebase.example.Main'
8+
9+
sourceCompatibility = 1.8
10+
11+
repositories {
12+
mavenCentral()
13+
mavenLocal()
14+
}
15+
16+
dependencies {
17+
// Firebase Admin Java SDK
18+
compile 'com.google.firebase:firebase-admin:5.1.0'
19+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.google.firebase.example;
2+
3+
import com.google.firebase.auth.FirebaseCredential;
4+
import com.google.firebase.auth.FirebaseCredentials;
5+
import com.google.firebase.auth.GoogleOAuthAccessToken;
6+
import com.google.firebase.internal.NonNull;
7+
import com.google.firebase.tasks.OnCompleteListener;
8+
import com.google.firebase.tasks.Task;
9+
10+
import java.io.FileInputStream;
11+
import java.io.FileNotFoundException;
12+
import java.io.IOException;
13+
14+
public class Main {
15+
16+
public void getServiceAccountAccessToken() throws FileNotFoundException, IOException{
17+
// [START get_service_account_tokens]
18+
FileInputStream serviceAccount = new FileInputStream("path/to/serviceAccountKey.json");
19+
FirebaseCredential credential = FirebaseCredentials.fromCertificate(serviceAccount);
20+
credential.getAccessToken().addOnCompleteListener(
21+
new OnCompleteListener<GoogleOAuthAccessToken>() {
22+
@Override
23+
public void onComplete(@NonNull Task<GoogleOAuthAccessToken> task) {
24+
if (task.isSuccessful()) {
25+
String accessToken = task.getResult().getAccessToken();
26+
long expirationTime = task.getResult().getExpiryTime();
27+
// Attach accessToken to HTTPS request in the
28+
// "Authorization: Bearer" header
29+
// After expirationTime. you must generate a new access
30+
// token
31+
} else {
32+
// Error
33+
}
34+
}});
35+
// [END get_service_account_tokens]
36+
}
37+
38+
public static void main(String[] args) {
39+
// write your code here
40+
}
41+
}

settings.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
include ':database'
1+
include ':admin'
2+
include ':database'

0 commit comments

Comments
 (0)