I try to receive the list of google task from react native app.
I created a new app :
npx react-native init GoogleTasksApp
Added the package :
npm install @react-native-google-signin/google-signin axios
And rewrite the App.tsx
:
import React, {useEffect, useState} from 'react';
import {View, Button, Text, FlatList} from 'react-native';
import {
GoogleSignin,
statusCodes,
} from '@react-native-google-signin/google-signin';
import axios from 'axios';
const GOOGLE_WEB_CLIENT_ID =
'38819132xxxxxxxxxxxxxxxxxxxxogleusercontent.com';
const GOOGLE_ANDROID_CLIENT_ID =
'38819132xxxxxxxxxxxxxxxxxxxxs.googleusercontent.com';
const App = () => {
const [userInfo, setUserInfo] = useState(null);
const [tasks, setTasks] = useState([]);
useEffect(() => {
GoogleSignin.configure({
scopes: [
'https://www.googleapis.com/auth/tasks',
'https://www.googleapis.com/auth/calendar',
],
webClientId: GOOGLE_WEB_CLIENT_ID,
});
}, []);
const signIn = async () => {
try {
await GoogleSignin.hasPlayServices();
const userInfo = await GoogleSignin.signIn();
setUserInfo(userInfo);
fetchTasks(userInfo.idToken);
} catch (error) {
console.log('Error :', error);
if (error.code === statusCodes.SIGN_IN_CANCELLED) {
console.log('User cancelled the login flow');
} else if (error.code === statusCodes.IN_PROGRESS) {
console.log('Sign in is in progress already');
} else if (error.code === statusCodes.PLAY_SERVICES_NOT_AVAILABLE) {
console.log('Play services not available or outdated');
} else {
console.log('Some other error happened:', error);
}
}
};
const fetchTasks = async token => {
try {
const response = await axios.get(
'https://www.googleapis.com/tasks/v1/users/@me/lists',
{
headers: {Authorization: `Bearer ${token}`},
},
);
setTasks(response.data.items);
} catch (error) {
console.log('Error fetching tasks:', error);
}
};
return (
<View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}>
{userInfo ? (
<View>
<Text>Welcome, {userInfo.user.name}</Text>
<FlatList
data={tasks}
keyExtractor={item => item.id}
renderItem={({item}) => <Text>{item.title}</Text>}
/>
</View>
) : (
<>
<Button title="Sign in with Google" onPress={signIn} />
</>
)}
</View>
);
};
export default App;
I can receive the user data, but when I try to get list of tasks, I get an http 401 error with this token.
Task API is enabled in Google console. I created OAuth credentials for web, Android try to use it – same http 401 error.
What do I need to do to get list of tasks, with what type of api_key? What do I need to change in Google console to make it work?
Maxim Uglov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.