【问题标题】:How to authentication user for uploading video in youtube channel flutter?如何验证用户以在 youtube 频道颤动中上传视频?
【发布时间】:2021-11-24 19:12:13
【问题描述】:

我想验证用户在他们的频道中上传视频,所需参数只是验证成功与否,目前我正在使用这 3 个插件google_sign_ingoogleapisextension_google_sign_in_as_googleapis_auth,通过这些我只能在firebase的帮助下登录用户的google账号。

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" 好心人帮忙,我只是想为他们的你管频道验证用户。

【问题讨论】:

    标签: flutter google-api youtube-data-api


    【解决方案1】:

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

    insufficient_scope

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

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

    你应该检查Google apis dart client library

    【讨论】:

      猜你喜欢
      • 2023-03-10
      • 2015-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-08
      • 2012-01-08
      • 1970-01-01
      • 2013-10-27
      相关资源
      最近更新 更多