我有一个服务帐户,我将一个Json文件下载到了我的android studio的“ assets”文件夹中。然后,我从Google自己的文档中复制/粘贴了代码,并得到null。
我尝试了各种方式来读取我的json文件,但都为空。
void AuthExplicit(){
GoogleCredentials credentials = null;
try {
credentials = GoogleCredentials.fromStream(new FileInputStream("auth.json"))
.createScoped(Lists.newArrayList(Collections.singleton("https://www.googleapis.com/auth/cloud-platform")));
}
catch (IOException e) {
e.printStackTrace();
}
new RefreshCred().execute(credentials);
token = credentials.getAccessToken();
tokenString = token.toString();
}
EDIT1 因此,我在res下创建了一个原始文件夹,并将auth.json文件放入其中。然后将我的代码更改为以下内容。凭据仍然为空。
void AuthExplicit(){
try
{
InputStream stream = getResources().openRawResource(R.raw.auth);
GoogleCredentials credentials = GoogleCredentials.fromStream(stream);
token = credentials.getAccessToken();
tokenString = token.toString();
Log.i("Token",tokenString);
new RefreshCred().execute(credentials);
}
catch (IOException e)
{
e.printStackTrace();
}
}