I am currently using Expo app auth, "https://docs.expo.io/versions/latest/sdk/app-auth/" for authentication of my app.
New to this React Native world, but i would like to request for userinfo from google Api which this method is using. I tried using postman, and it returns the values i would need.
How can i use access token to get request for userinfo from Google Api?
let config = {
issuer: 'https://accounts.google.com',
scopes: ['openid', 'profile'],
/ This is the CLIENT_ID generated from a Firebase project /
clientId: '603386649315-vp4revvrcgrcjme51ebuhbkbspl048l9.apps.googleusercontent.com',
};
let StorageKey = '@MyApp:CustomGoogleOAuthKey';
export async function signInAsync() {
let authState = await AppAuth.authAsync(config);
await cacheAuthAsync(authState);
console.log('signInAsync', authState);
return authState;
}
async function cacheAuthAsync(authState) {
return await AsyncStorage.setItem(StorageKey, JSON.stringify(authState));
}
export async function getCachedAuthAsync() {
let value = await AsyncStorage.getItem(StorageKey);
let authState = JSON.parse(value);
console.log('getCachedAuthAsync', authState);
if (authState) {
if (checkIfTokenExpired(authState)) {
return refreshAuthAsync(authState);
} else {
return authState;
}
}
return null;
}
function checkIfTokenExpired({ accessTokenExpirationDate }) {
return new Date(accessTokenExpirationDate) < new Date();
}
async function refreshAuthAsync({ refreshToken }) {
let authState = await AppAuth.refreshAsync(config, refreshToken);
console.log('refreshAuth', authState);
await cacheAuthAsync(authState);
return authState;
}
I am currently using Expo app auth, "https://docs.expo.io/versions/latest/sdk/app-auth/" for authentication of my app. New to this React Native world, but i would like to request for userinfo from google Api which this method is using. I tried using postman, and it returns the values i would need. How can i use access token to get request for userinfo from Google Api?
The link below returned every value i needed, with bearer token of course. 'https://www.googleapis.com/oauth2/v2/userinfo'
Here is the code:
import React, { useEffect, useState } from 'react'; import { AsyncStorage, Button, StyleSheet, Text, View } from 'react-native'; import * as AppAuth from 'expo-app-auth';
export default function App() { let [authState, setAuthState] = useState(null);
useEffect(() => { (async () => { let cachedAuth = await getCachedAuthAsync(); if (cachedAuth && !authState) { setAuthState(cachedAuth); } })(); }, []);
return (
); }
const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#fff', alignItems: 'center', justifyContent: 'center', }, });
let config = { issuer: 'https://accounts.google.com', scopes: ['openid', 'profile'], / This is the CLIENT_ID generated from a Firebase project / clientId: '603386649315-vp4revvrcgrcjme51ebuhbkbspl048l9.apps.googleusercontent.com', };
let StorageKey = '@MyApp:CustomGoogleOAuthKey';
export async function signInAsync() { let authState = await AppAuth.authAsync(config); await cacheAuthAsync(authState); console.log('signInAsync', authState); return authState; }
async function cacheAuthAsync(authState) { return await AsyncStorage.setItem(StorageKey, JSON.stringify(authState)); }
export async function getCachedAuthAsync() { let value = await AsyncStorage.getItem(StorageKey); let authState = JSON.parse(value); console.log('getCachedAuthAsync', authState); if (authState) { if (checkIfTokenExpired(authState)) { return refreshAuthAsync(authState); } else { return authState; } } return null; }
function checkIfTokenExpired({ accessTokenExpirationDate }) { return new Date(accessTokenExpirationDate) < new Date(); }
async function refreshAuthAsync({ refreshToken }) { let authState = await AppAuth.refreshAsync(config, refreshToken); console.log('refreshAuth', authState); await cacheAuthAsync(authState); return authState; }
export async function signOutAsync({ accessToken }) { try { await AppAuth.revokeAsync(config, { token: accessToken, isClientIdProvided: true, }); await AsyncStorage.removeItem(StorageKey); return null; } catch (e) { alert(
Failed to revoke token: ${e.message}
); } }