Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android giving IOException with 'unable to create directory: /tokens' when using google calendar api

Trying to implement the Calendar Quickstart API into Android but when I declare tokens as demonstrated. private final String TOKENS_DIRECTORY_PATH = "tokens";

That String is then used in the builder

GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
                HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
                .setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
                .setAccessType("offline")
                .build();

But Android returns this error when setting the DataStoreFactory

java.io.IOException: unable to create directory: /tokens

Is there a different method to creating a directory that will work? or must I change the file path of TOKENS_DIRECTORY_PATH?

like image 232
T.kat Avatar asked Oct 28 '25 08:10

T.kat


1 Answers

I used this piece of code.

File tokenFolder = new File(Environment.getExternalStorageDirectory() +
            File.separator + TOKENS_DIRECTORY_PATH);
    if (!tokenFolder.exists()) {
        tokenFolder.mkdirs();
    }

    flow = new GoogleAuthorizationCodeFlow.Builder(
            HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
            .setDataStoreFactory(new FileDataStoreFactory(tokenFolder))
            .setAccessType("offline")
            .build();

And get permissions to external storage in Android manifest file

EDIT: The methods specified in Google API documentation for Java doesn't seem to work well for Android. Use this github project as a guide for implementing integrating Google APIs into Android applications.

like image 103
Charan M Avatar answered Oct 29 '25 23:10

Charan M



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!