0

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();
        }

    }
Turner Allen
  • 43
  • 1
  • 6

1 Answers1

0

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.

RaStall
  • 55
  • 1
  • 4