Youtube Login Api OAuth 2.0 flutter - flutter

I've been trying for a bit to implement login of youtube channels on my app. but all I could do was google accounts with google_sign_in 4.5.1
has any of you implemented this feature and can give me an hint? youtube docs are terrible
edit:
here's the code
class AuthService {
String oAuthClientId = '**hidden**'; // not used?
static List<String> scopes = [
'email',
'https://www.googleapis.com/auth/youtube', // Youtube scope
];
final FirebaseAuth auth = FirebaseAuth.instance;
GoogleSignIn googleSignIn = GoogleSignIn(
scopes: scopes,
);
Future<FirebaseUser> login() async {
final GoogleSignInAccount googleSignInAccount = await googleSignIn.signIn();
final GoogleSignInAuthentication googleSignInAuthentication = await googleSignInAccount.authentication;
final AuthCredential credential = GoogleAuthProvider.getCredential(
accessToken: googleSignInAuthentication.accessToken,
idToken: googleSignInAuthentication.idToken,
);
final token = googleSignInAuthentication.accessToken;
final AuthResult authResult = await auth.signInWithCredential(credential);
final FirebaseUser user = authResult.user;
assert(!user.isAnonymous);
assert(await user.getIdToken() != null);
final FirebaseUser currentUser = await auth.currentUser();
assert(user.uid == currentUser.uid);
var client = new http.Client();
return currentUser;
}
void signOut() {
auth.signOut();
googleSignIn.disconnect();
}
Future<FirebaseUser> getUser() async{
final FirebaseUser user = await auth.currentUser();
return user;
}
}

To display your youtube channels, you'll need to add the youtube scope https://www.googleapis.com/auth/youtube to your google login.
Prior to this, you must've setup enabled the Youtube api from your developer console. Read the Youtube API documentation for more information
Example login code:
login() async {
GoogleSignIn googleSignIn = GoogleSignIn(
scopes: [
'email',
'https://www.googleapis.com/auth/youtube', // Youtube scope
],
);
final GoogleSignInAccount googleSignInAccount = await googleSignIn.signIn();
final GoogleSignInAuthentication googleSignInAuthentication =
await googleSignInAccount.authentication;
final AuthCredential credential = GoogleAuthProvider.getCredential(
accessToken: googleSignInAuthentication.accessToken,
idToken: googleSignInAuthentication.idToken,
);
// You'll need this token to call the Youtube API. It expires every 30 minutes.
final token = googleSignInAuthentication.accessToken;
final AuthResult authResult = await auth.signInWithCredential(credential);
final FirebaseUser user = authResult.user;
assert(!user.isAnonymous);
assert(await user.getIdToken() != null);
final FirebaseUser currentUser = await auth.currentUser();
assert(user.uid == currentUser.uid);
}

Related

flutter add user data after google sign in

i have a problem with save user data to firestore i using sign in with google auth so after i want to add user data to firestore so i can not do this can you help me thanks.
this is my auth code
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:google_sign_in/google_sign_in.dart';
class GoogleSignProvider extends ChangeNotifier{
final googleSignIn = GoogleSignIn();
GoogleSignInAccount? _user;
GoogleSignInAccount? get user => _user;
final FirebaseAuth _auth = FirebaseAuth.instance;
bool result = false;
Future googleLogin()async {
try {
final googleUSer = await googleSignIn.signIn();
if (googleUSer == null ) return;
_user = googleUSer;
final FirebaseFirestore _firestore = FirebaseFirestore.instance;
final googleAuth = await googleUSer.authentication;
final credential = GoogleAuthProvider.credential(
accessToken: googleAuth.accessToken,
);
await FirebaseAuth.instance.signInWithCredential(credential);
UserCredential userCredential = await _auth.signInWithCredential(credential);
User? user = userCredential.user;
if (user != null){
if (userCredential.additionalUserInfo!.isNewUser) {
await _firestore.collection('users').doc(user.uid).set(
{
'username': user.displayName,
'uid': user.uid,
'profilePhoto': user.photoURL,
}
);
}}
return result;
} catch (e) {
print(e.toString());
}
notifyListeners();
}
Future logout() async {
await googleSignIn.disconnect();
FirebaseAuth.instance.signOut();
}
}
Hey were you able to save the user to firestore? I have pasted the answer that worked for me.
final userGoogle = FirebaseAuth.instance.currentUser!;
await _firestore
.collection('users')
.doc(cred.user!.uid)
.set({
uid: userGoogle.uid,
name: userGoogle.displayName!,
email: userGoogle.email!,
photoURL: userGoogle.photoURL!,
});
I'm curious if this would mean that every time I login using the google account, the photo from the Google account would overwrite the photoURL, assuming it was updated from let's say the profile page. Could help little insight into it.

Flutter Google Sign In error after canceling Choose an account pop-up

After clicking sign in, choose an account pop comes up but if the user pressed back or outside of the pop-up it throws an error.
Error -
Sign in class -
class GoogleSignInProvider extends ChangeNotifier {
final googleSignIn = GoogleSignIn();
GoogleSignInAccount? _user;
GoogleSignInAccount get user => _user!;
Future googleLogin() async {
await googleSignIn.signOut();
final googleUser = await googleSignIn.signIn();
if (googleUser == null) return;
_user = googleUser;
final googleAuth = await googleUser.authentication;
final credential = GoogleAuthProvider.credential(
accessToken: googleAuth.accessToken, idToken: googleAuth.idToken);
await FirebaseAuth.instance.signInWithCredential(credential);
notifyListeners();
}
}
Sign out button -
TextButton(
onPressed: () async {
final googleCurrentUser = GoogleSignIn().currentUser;
if (googleCurrentUser != null) {
await GoogleSignIn().disconnect();
}
FirebaseAuth.instance.signOut();
Navigator.pop(context);
},
child: const Text(
'Yes',
style: TextStyle(color: primaryColor),
))
Instead of if (googleUser == null) return; try using if (googleUser == null) return null;.
Here is my sign in method;
Future<UserCredential?> signInWithGoogle() async {
final _googleSignIn = GoogleSignIn();
await _googleSignIn.disconnect().catchError((e, stack) {
print(e);
});
final GoogleSignInAccount? googleUser = await _googleSignIn.signIn();
// handling the exception when cancel sign in
if (googleUser == null) return null;
// Obtain the auth details from the request
final GoogleSignInAuthentication? googleAuth =
await googleUser.authentication;
// Create a new credential
final credential = GoogleAuthProvider.credential(
accessToken: googleAuth?.accessToken,
idToken: googleAuth?.idToken,
);
return await FirebaseAuth.instance.signInWithCredential(credential);
}
The sign out method;
Future signOut() async {
var result = await FirebaseAuth.instance.signOut();
return result;
}
This only happens in debugging, in release this won't be an issue.

How can i get the access token through google sign in -- Flutter

I want to get the access token from google sign and save it as a variable so I'll be able to push it to my own API , this question has been asked before but the answers are outdated . I'm using this package :Google sign in
her,s my log in code that i access through a button :
GestureDetector(
onTap: () {
_googleSignIn.signIn().catchError((e) {
print('Erorr');
});
;
},
To get access token when logged in with google use
final credential = GoogleAuthProvider.credential(
accessToken: googleAuth?.accessToken,
idToken: googleAuth?.idToken,
);
Full method:
import 'package:google_sign_in/google_sign_in.dart';
Future<UserCredential> signInWithGoogle() async {
// Trigger the authentication flow
final GoogleSignInAccount? googleUser = await GoogleSignIn().signIn();
// Obtain the auth details from the request
final GoogleSignInAuthentication? googleAuth = await googleUser?.authentication;
// Create a new credential
final credential = GoogleAuthProvider.credential(
accessToken: googleAuth?.accessToken,
idToken: googleAuth?.idToken,
);
// Once signed in, return the UserCredential
return await FirebaseAuth.instance.signInWithCredential(credential);
}
for document https://firebase.flutter.dev/docs/auth/social/
Future<bool> google() async {
try {
final googleUser = await googleSignIn.signIn();
final googleAuth = await googleUser.authentication;
final AuthCredential credential = GoogleAuthProvider.credential(
accessToken: googleAuth?.accessToken,// accessToken
idToken: googleAuth?.idToken,
);
users = (await auth.signInWithCredential(credential)).user;
if (users == null) {
return false;
}
return true;
} catch (e) {
print('this is error .......$e');
return null;
}
you can get the access token
googleAuth.accessToken //fellowing my code
Future<FirebaseUser> signInWithGoogle(SignInViewModel model) async {
model.state =ViewState.Busy;
GoogleSignInAccount googleSignInAccount = await _googleSignIn.signIn();
GoogleSignInAuthentication googleSignInAuthentication =
await googleSignInAccount.authentication;
AuthCredential credential = GoogleAuthProvider.getCredential(
accessToken: googleSignInAuthentication.accessToken,
idToken: googleSignInAuthentication.idToken,
);
AuthResult authResult = await _auth.signInWithCredential(credential);
_user = authResult.user;
assert(!_user.isAnonymous);
assert(await _user.getIdToken() != null);
FirebaseUser currentUser = await _auth.currentUser();
assert(_user.uid == currentUser.uid);
model.state =ViewState.Idle;
print("User Name: ${_user.displayName}");
print("User Email ${_user.email}");
}

'https://www.googleapis.com/auth/contacts.readonly' not working showing loading... in google sign

Future googleSignIn() async {
final GoogleSignInAccount googleUser = await GoogleSignIn(
scopes: ['https://www.googleapis.com/auth/contacts.readonly']).signIn();
final GoogleSignInAuthentication googleAuth =
await googleUser.authentication;
final credential = GoogleAuthProvider.credential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
await FirebaseAuth.instance.signInWithCredential(credential);
}
google API is not working

How to get additional scopes from GoogleSignIn in Flutter?

I have a method which I am using to get a users basic info like birthday, gender & phone number and I'm now sure how to implement it in Flutter?
void signInWithGoogle(context) async {
final GoogleSignIn _googleSignIn = GoogleSignIn();
try {
final GoogleSignInAccount googleUser = await _googleSignIn.signIn();
final GoogleSignInAuthentication googleAuth =
await googleUser.authentication;
final AuthCredential credential = GoogleAuthProvider.credential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
final User user = (await _auth.signInWithCredential(credential)).user;
} catch (error) {
print('Google error: $error');
}
}
I hope this answer will help you.
You can try adding a scope in your GoogleSignIn() method like that
GoogleSignIn _googleSignIn = GoogleSignIn(
scopes: [
'email',
'https://www.googleapis.com/auth/contacts.readonly',
],
);
And you can adjust your scopes based on the information you want to access.
And this is the link to the full list of scopes you can use Google Scopes list
Note that some scopes will require app verification and it's mentioned in the above link.