YouTube登录Api OAuth 2.0扑朔迷离 [英] Youtube Login Api OAuth 2.0 flutter
问题描述
我一直在尝试在我的应用程序上实现YouTube频道的登录.但我所能做的就是使用 google_sign_in 4.5.1
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
你们中的任何人都实现了此功能,并且可以给我提示吗?youtube文档太糟糕了
has any of you implemented this feature and can give me an hint? youtube docs are terrible
这是代码
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;
}
}
推荐答案
要显示您的YouTube频道,您需要添加YouTube范围 https://www.googleapis.com/auth/youtube
到您的Google登录名.
To display your youtube channels, you'll need to add the youtube scope https://www.googleapis.com/auth/youtube
to your google login.
在此之前,您必须先通过开发人员控制台设置启用Youtube api 一个>.阅读 Youtube API文档以获取更多信息
Prior to this, you must've setup enabled the Youtube api from your developer console. Read the Youtube API documentation for more information
示例登录代码:
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);
}
这篇关于YouTube登录Api OAuth 2.0扑朔迷离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!