flutter - 如何验证用户以在 youtube 频道颤动中上传视频?
问题描述
我想验证用户在他们的频道中上传视频,所需参数只是验证成功与否,目前我正在使用这 3 个插件google_sign_in
,googleapis
extension_google_sign_in_as_googleapis_auth
通过这些我只能在 的帮助下登录用户的谷歌帐户firebase
。
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:extension_google_sign_in_as_googleapis_auth/extension_google_sign_in_as_googleapis_auth.dart';
import 'package:googleapis/youtube/v3.dart';
GoogleSignIn _googleSignIn = GoogleSignIn(
scopes: <String>[
'email',
//'https://www.googleapis.com/auth/youtube.readonly',
],
);
void main() {
runApp(
MaterialApp(
title: 'Google Sign In',
home: SignInDemo(),
),
);
}
class SignInDemo extends StatefulWidget {
@override
State createState() => SignInDemoState();
}
class SignInDemoState extends State<SignInDemo> {
GoogleSignInAccount? _currentUser;
String? _contactText;
@override
void initState() {
super.initState();
_googleSignIn.onCurrentUserChanged.listen((GoogleSignInAccount? account) async {
setState(() {
_currentUser = account;
});
if (_currentUser != null) {
_handleGetChannels();
}
});
_googleSignIn.signInSilently();
}
Future<void> _handleGetChannels() async {
setState(() {
_contactText = 'Loading subscription info...';
});
var httpClient = (await _googleSignIn.authenticatedClient())!;
print("hello${httpClient.credentials.accessToken}");
var youTubeApi = YouTubeApi(httpClient);
var favorites = await youTubeApi.playlistItems.list(
['snippet'],
playlistId: 'LL', // Liked List
);
print("hey $favorites");
// final youtubeApi = YouTubeApi(await _googleSignIn.authenticatedClient());
// final response = await youtubeApi.subscriptions.list('snippet', mine: true);
setState(() {
if (favorites.items!.isNotEmpty) {
final channels =
favorites.items!.map((sub) => sub.snippet!.title).join(', ');
_contactText = 'I see you follow: ${channels}!';
} else {
_contactText = 'No channels to display.';
}
});
}
Future<void> _handleSignIn() async {
try {
await _googleSignIn.signIn();
} catch (error) {
print(error);
}
}
Future<void> _handleSignOut() => _googleSignIn.disconnect();
Widget _buildBody() {
if (_currentUser != null) {
return Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
ListTile(
leading: GoogleUserCircleAvatar(
identity: _currentUser!,
),
title: Text(_currentUser!.displayName ?? ''),
subtitle: Text(_currentUser!.email),
),
const Text('Signed in successfully.'),
Text(_contactText ?? ''),
RaisedButton(
child: const Text('SIGN OUT'),
onPressed: _handleSignOut,
),
RaisedButton(
child: const Text('REFRESH'),
onPressed: _handleGetChannels,
),
],
);
} else {
return Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
const Text('You are not currently signed in.'),
RaisedButton(
child: const Text('SIGN IN'),
onPressed: _handleSignIn,
),
],
);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Google Sign In'),
),
body: ConstrainedBox(
constraints: const BoxConstraints.expand(),
child: _buildBody(),
));
}
}
如果我在范围内传递“https://www.googleapis.com/auth/youtube.readonly”,它只显示加载,并且从电子邮件范围内我得到了错误Unhandled Exception: Access was denied (www-authenticate header was: Bearer realm="https://accounts.google.com/", error="insufficient_scope"
,请帮助,我只想为他们的你管频道验证用户。
解决方案
YouTube 数据 API支持用于授权访问私人用户数据的 OAuth 2.0 协议。
范围不足
表示您发送的访问令牌未在您调用的方法所需的范围内获得授权。
例如video.insert需要以下范围之一的授权
您应该检查Google apis 飞镖客户端库
推荐阅读
- node.js - net::ERR_CERT_AUTHORITY_INVALID(与节点反应)
- java - 在 Jenkins 上运行测试时,AspectJ 编织异常(org.aspectj.weaver.BCException:Whilst processing type..)
- python - 使用 pyodbc、存储过程和 SQL Server 执行
- java - 在 SpringBoot 测试结果中使用 ApplicationContextRunner 模拟依赖项到 UnsatisfiedDependencyException
- azure-container-service - 从云外壳创建 ACI 时,我可以创建新的 Azure ML 工作区吗
- c - libsndfile 无法使用浮点数组编码 WAV
- c - 从套接字缓冲区检索 TCP MSS 值
- discord.js - 在分片中使用他的 ID 获取用户标签?不和谐.js
- java - Spring Data JPA - NUMBER 列类型的空参数问题
- twilio - 用于会议的异步 AMD