首页 > 解决方案 > 如何验证用户以在 youtube 频道颤动中上传视频?

问题描述

我想验证用户在他们的频道中上传视频,所需参数只是验证成功与否,目前我正在使用这 3 个插件google_sign_ingoogleapis 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" ,请帮助,我只想为他们的你管频道验证用户。

标签: fluttergoogle-apiyoutube-data-api

解决方案


YouTube 数据 API支持用于授权访问私人用户数据的 OAuth 2.0 协议。

范围不足

表示您发送的访问令牌未在您调用的方法所需的范围内获得授权。

例如video.insert需要以下范围之一的授权

在此处输入图像描述

您应该检查Google apis 飞镖客户端库


推荐阅读