如何使用访问令牌访问firebase rest api?

时间:2017-12-07 02:56:50

标签: java android firebase-realtime-database

我正在尝试检索访问令牌以访问FIREBASE REST API。根据此链接:Authenticate REST Requests我下载了服务帐户密钥文件。然后我将其粘贴到android项目的app目录中。

enter image description here

但每次在FileInputStream中获取null。下面的其余Java代码:

FileInputStream serviceAccount = null;
try {
    serviceAccount = new FileInputStream("pooka-166905-firebase-adminsdk-fmil7-99eadf997d.json");
} catch (FileNotFoundException e) {
    e.printStackTrace();
}

GoogleCredential googleCred = null;
try {
    googleCred = GoogleCredential.fromStream(serviceAccount);
} catch (IOException e) {
    e.printStackTrace();
}

GoogleCredential scoped = googleCred.createScoped(
        Arrays.asList(
                "https://www.googleapis.com/auth/firebase.database",
                "https://www.googleapis.com/auth/userinfo.email"
        )
);

try {
    scoped.refreshToken();
} catch (IOException e) {
    e.printStackTrace();
}
String token = scoped.getAccessToken();

0 个答案:

没有答案