Skip to content

Instantly share code, notes, and snippets.

@surajp
Last active December 28, 2020 21:11
Show Gist options
  • Save surajp/4f345d2ee5274d5fd6d6536b909050a4 to your computer and use it in GitHub Desktop.
Save surajp/4f345d2ee5274d5fd6d6536b909050a4 to your computer and use it in GitHub Desktop.
Google OAuth using LWC + VF
import { LightningElement } from 'lwc';
const BASE_URL='https://accounts.google.com/o/oauth2/v2/auth';
export default class Goauth extends LightningElement {
doAuth(){
let client_id='<your client id>';
console.log('test');
let redirect_uri=`${window.location.origin}/apex/oauthbroker`;
let scope='https%3A//www.googleapis.com/auth/drive.metadata.readonly';
let url=`${BASE_URL}?scope=${scope}&client_id=${client_id}&redirect_uri=${redirect_uri}&response_type=token`;
window.open(url);
window.onmessage = msg=>{
console.log('>> message '+msg.data); //msg.data contains the access token
}
}
}
<apex:page>
<script>
if(window.location.hash){
window.opener.postMessage(window.location.hash.substring(1),'<your-lightning-domain>');
}
</script>
</apex:page>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment