55 lines
1.9 KiB
JavaScript
55 lines
1.9 KiB
JavaScript
const scripts = ['https://apis.google.com/js/api.js', 'https://accounts.google.com/gsi/client'];
|
|
const DISCOVERY_DOC = 'https://www.googleapis.com/discovery/v1/apis/drive/v3/rest';
|
|
const CLIENT_ID = '53206975661-ih3r0ubph3rqejdq97b029difbrk2bqj.apps.googleusercontent.com';
|
|
|
|
// Authorization scopes required by the API; multiple scopes can be
|
|
// included, separated by spaces.
|
|
const SCOPES = 'openid email profile https://www.googleapis.com/auth/drive.appdata';
|
|
|
|
export class GoogleClient {
|
|
async initGApi() {
|
|
return new Promise((resolve, reject) => {
|
|
gapi.load('client', async () => {
|
|
await gapi.client.init({
|
|
discoveryDocs: [DISCOVERY_DOC],
|
|
});
|
|
resolve();
|
|
});
|
|
});
|
|
}
|
|
async initGoolgeClient() {
|
|
await Promise.all([loadSingleScript([scripts[0]]), loadSingleScript([scripts[1]])]);
|
|
await this.initGApi();
|
|
|
|
this.tokenClient = google.accounts.oauth2.initTokenClient({
|
|
client_id: CLIENT_ID,
|
|
scope: SCOPES,
|
|
callback: '', // defined later
|
|
});
|
|
console.log('google client init success');
|
|
}
|
|
login(funid) {
|
|
this.tokenClient.callback = async (resp) => {
|
|
if (resp.error !== undefined) {
|
|
throw resp;
|
|
}
|
|
console.log('token: ', gapi.client.getToken());
|
|
this.access_token = gapi.client.getToken().access_token;
|
|
let result = {
|
|
data: gapi.client.getToken().access_token,
|
|
errcode: 0,
|
|
};
|
|
jc.wallet.nativeSvr.handleNativeCallback(funid, JSON.stringify(result));
|
|
};
|
|
|
|
if (gapi.client.getToken() === null) {
|
|
// Prompt the user to select a Google Account and ask for consent to share their data
|
|
// when establishing a new session.
|
|
this.tokenClient.requestAccessToken({ prompt: 'consent' });
|
|
} else {
|
|
// Skip display of account chooser and consent dialog for an existing session.
|
|
this.tokenClient.requestAccessToken({ prompt: '' });
|
|
}
|
|
}
|
|
}
|