Sindy
Sindy

Reputation: 45

How to access google spreadsheet from android app without OAuth?

I'm in the process of connecting my Google Spreadsheet in Google Docs, to my android app using Google Sheets API.

I want to connect a single public spreadsheet as a database for my app. I was wondering how to do this without OAuth. I just want whoever has the app to be able to access/read from the database.

I was wondering how to do this?

I've followed instructions from Google Spreadsheet API page.

Also, how does the following URL help me access my public google spreadsheet? following URL (taken from google sheets api page)

https://spreadsheets.google.com/feeds/worksheets/key/private/full

my public Google spreadsheet: here

Upvotes: 4

Views: 3781

Answers (2)

Stephen Kittelson
Stephen Kittelson

Reputation: 146

Update: this actually requires an OAuth token setup in the Google Drive API console with the fingerprint of the key used to sign the app and the package name of the app.

It's possible to use the Google account of the phone's user without any generated OAuth tokens or credentials which may get through any authorization barriers preventing access to the spreadsheet:

First, setup the sign-in client:

    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
        .requestScopes(new Scope("https://www.googleapis.com/auth/spreadsheets"))
        .requestEmail()
        .build();
    googleSignInClient = GoogleSignIn.getClient(this, gso);

If the user isn't already signed in, then prompt the user to sign in (prompting to sign in when they are already signed in will result in a cancelled error):

    if (GoogleSignIn.getLastSignedInAccount(this) == null) {
        startActivityForResult(googleSignInClient.getSignInIntent(), RC_SIGN_IN);
    } else {
        // use the already signed in account
        Account account = GoogleSignIn.getLastSignedInAccount(this).getAccount();
    }

If the user was prompted to sign in, then after that activity is complete:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RC_SIGN_IN) {
        try {
            Account account = GoogleSignIn.getSignedInAccountFromIntent(data).getResult(ApiException.class).getAccount();
        } catch (ApiException e) {
            Log.w("MyActivity", "signInResult: failed code=" + e.getStatusCode() + ", reason: " + GoogleSignInStatusCodes.getStatusCodeString(e.getStatusCode()), e);
        }
    }
}

Then to use the sheets API using the user's account without any special OAuth token:

    GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(context, Collections.singleton("https://www.googleapis.com/auth/spreadsheets"));
    credential.setSelectedAccount(account);
    Sheets sheetsService = new Sheets.Builder(AndroidHttp.newCompatibleTransport(), JacksonFactory.getDefaultInstance(), credential).build();
    Spreadsheet response = null;
    try {
        response = sheetsService.spreadsheets().get("insert spreadsheet ID here").setRanges(Arrays.asList("'sheet name here'!A2:G30"))
                .setFields("sheets.data.rowData.values.effectiveValue").execute();
    } catch (UserRecoverableAuthIOException ex) {
        context.startActivity(ex.getIntent());
    } catch (IOException e) {
        Log.e(CLASS_NAME, "failure to get spreadsheet: " + e.getMessage(), e);
    }

Full project accessing Google Sheets using the user's OAuth instead of generating any tokens is here: https://github.com/stephenkittelson/interviewsetter

Upvotes: 9

CᴴᴀZ
CᴴᴀZ

Reputation: 531

Use Google SpreadSheet Library for Android by Prasanta for easy access of Sheets data. You can find the list of supported features on his blog.

OR

Access Sheets data as JSON and parse it in your app.

Access JSON using this URL: https://spreadsheets.google.com/tq?key=<your-sheets-key>

E.g.: https://spreadsheets.google.com/tq?key=1tJ64Y8hje0ui4ap9U33h3KWwpxT_-JuVMSZzxD2Er8k

Upvotes: 2

Related Questions