'Why is GoogleCredentials.fromStream(myJson) coming out null?
I have a service account for which I downloaded a Json file to my android studio's 'assets' folder. I then copy/pasted code from google's own documentation and am getting null.
I have tried various ways to read my json file, but all are null.
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 So I have created a raw folder under res and put my auth.json file in there. Then changed my code to the below. credentials is still null.
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();
}
}
Solution 1:[1]
You should extract the asset to some directory on the phone instead of loading it directly from the assets folder. For more details check this answer.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | RaStall |
