Pre Development
-
Create project
- get project no:(to be set in Appscript Settings)
- create web client
- Get Client_ID(used in js, PHP and android app) and Client Secret(used in PHP ie backend app only) from Web client
- Add required scopes
- For JS ,get API key(used in javascript while intitializing
gapi
client (gapi.client.init). - For Backend(PHP), Set Redirect URLs( for backend script to recieve Auth Code)
- For android, create SHA-1 certificate and set.
-
Create App Script
- go to settings, set project number got above
- deploy as API executable
- get script ID. For API Executable scripts, this is deployment_Id in Manage Deployments. Used in js:
gapi.client.request
and PHP:$service->scripts->run
andmService.scripts().run(scriptId, request).execute();
in Android - Test directly to make sure there are no errors in the script.
PHP
- download Google API Client Library with composer
composer require google/apiclient:^2.15.0
and
require_once '/path/to/your-project/vendor/autoload.php';
- Declare credentials
- Client ID
- Client Secret
- Script ID
- SCOPES
- Initiate
GoogleClient
with correct redirect URL and scopes - Create login link
- Create method
fetchTokenFromAuthCode
to get toke from code- token so got is JSON object will contain the following
- access_token
- expires_in
- refresh_token (first time login only)
- scope
- token_type
- id_token (only if profile and email scopes are requested)
- created
{ "access_token":"ya29.a0AfB_byBHMAjgaqzwtbS5lfvUGNESM_Hy1ebxWOoPbv6mtg5hO_1p343wNW6mBZvtexAvr9Je8HSO7x8q2Lsq1k_k6GiPtMowlkTUKPiUZrya9vJvbXUnw3vd9jf2J0j5LVa2VvQKK8LolYkAPGDk42no2f0JHTFV4gaCgYKAYQSARESFQGOcNnCKwegBmqHjgJqHhq6WkpNxQ0169", "expires_in":3599, "refresh_token":"1\/\/0gq2Nvl4NsFmxCgYIARAAGBASNwF-L9Ircs4nklgMCndIjnB1pnfafJrj7v4MPXew1NT-WXnGzrCCQ6NpwpfIfABIqAcY4Spsq98", "scope":"https:\/\/www.googleapis.com\/auth\/drive https:\/\/www.googleapis.com\/auth\/userinfo.profile https:\/\/www.googleapis.com\/auth\/userinfo.email https:\/\/www.googleapis.com\/auth\/script.external_request https:\/\/www.googleapis.com\/auth\/calendar.readonly openid", "token_type":"Bearer", "id_token":"eyJhbGciOiJSUzI1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nj63YAjKaxZLCdCMG3ZdkXnMJguZ5oUeG0zEawCl7cI3HCLRsGL4C0q9T9oft1RYFVHtm0jQ", "created":1694155573}
- token so got is JSON object will contain the following
- Initiate
GoogleService
with will be used to call API functions - create
setAccessToken
method to refresh access_token if expired($this->googleClient->refreshToken
). - Before calling API fucntion
setAccessToken
. - call API function
- get result and display it
Javascript
- Declare credentials.
- CLIENT_ID
- CLIENT_SECRET
- scriptId
- SCOPES
- initialize
gis client
. there are 2 ways- Implicit method
tokenClient = google.accounts.oauth2.initTokenClient
(access key got directly in js. but refresh key will not be got). - Authorisation Code model
client = google.accounts.oauth2.initCodeClient
(auth code recieved in JS. sent to backend which obtains token with refresh_token
- Implicit method
- Initialize
gapi.client
- get access key with either method above
- before API function call
gapi.client.setToken
- call API function
gapi.client.request
- get result and display it
Android
JAVA API for Google AppScripts
Getting Auth Code to get Refresh token from WebClient
- declare credentials
- CLIENT_ID
- scriptId
- SCOPES
- initiate
GoogleAccountCredential mCredential
objectmCredential = GoogleAccountCredential.usingOAuth2( getApplicationContext(), Arrays.asList(SCOPES)) .setBackOff(new ExponentialBackOff());
- Choose Account
private void chooseAccount() {
startActivityForResult(
mCredential.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER);
}
- Get Token in
onActivityResult
new Thread( () -> { try { String token = mCredential.getToken(); } catch (IOException e) { throw new RuntimeException(e); } catch (GoogleAuthException e) { throw new RuntimeException(e); } }).start();
new MakeRequestTask(mCredential).execute();
|
|