【问题标题】:Access Youtube data API through Flutter app with OAuth2使用 OAuth2 通过 Flutter 应用访问 Youtube 数据 API
【发布时间】:2020-11-10 04:54:18
【问题描述】:

我的 Flutter 应用正在使用 Firebase 进行 Google 身份验证和数据库,它需要与 Youtube Data Api v.3 通信,以便代表用户上传视频。似乎功能最强大的库是 googleapis: ^0.55.0googleapis_auth: ^0.2.12,但是重定向 url 存在问题,因为它们不支持它。
在同意屏幕中单击“允许”后,按照 [提供的示例][1](已安装/控制台应用程序)实施代码会导致控制台中出现以下错误:

E/flutter (18433): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: NoSuchMethodError: The getter 'length' was called on null.
E/flutter (18433): Receiver: null
E/flutter (18433): Tried calling: length
E/flutter (18433): #0      Object.noSuchMethod  (dart:core-patch/object_patch.dart:53:5)
E/flutter (18433): #1      _Uri._uriEncode  (dart:core-patch/uri_patch.dart:46:23)
E/flutter (18433): #2      Uri.encodeQueryComponent  (dart:core/uri.dart:1105:17)
E/flutter (18433): #3      obtainAccessCredentialsUsingCode 
        package:googleapis_auth/…/oauth2_flows/auth_code.dart:61
E/flutter (18433): #4      AuthorizationCodeGrantAbstractFlow._obtainAccessCredentialsUsingCode 
        package:googleapis_auth/…/oauth2_flows/auth_code.dart:128
E/flutter (18433): #5      AuthorizationCodeGrantServerFlow.run 
        package:googleapis_auth/…/oauth2_flows/auth_code.dart:207
E/flutter (18433): <asynchronous suspension>
E/flutter (18433): #6      obtainAccessCredentialsViaUserConsent 
        package:googleapis_auth/auth_io.dart:285

在模拟器中我收到一个 http 错误 500(localhost 无法处理请求),但查看返回 URI,它似乎包含授权代码。关于如何将授权码从 uri 取回应用程序的任何想法?现在我正在手动将 auth url 复制到浏览器中以获取同意屏幕,因此我正在寻找一个处理该问题的实现。 这是我当前的代码:

        import "package:http/http.dart" as http;
        import "package:googleapis_auth/auth_io.dart";
        import 'package:oauth2/oauth2.dart';
        var id = new ClientId(
            "myclientid.apps.googleusercontent.com",
            null);
        var scopes = [
          "https://www.googleapis.com/auth/youtube",
          "https://www.googleapis.com/auth/youtube.upload",
        ];
        var client = new http.Client();
        getall() {
          obtainAccessCredentialsViaUserConsent(id, scopes, client, prompt)
              .then((AccessCredentials credentials) {
             //I guess I should be using the credentials here.
            client.close();
          });
        }
        prompt(String link) async {
          print("link $link");//I use this to manually copy/paste into the browser
          print("we got this far");
        }

任何输入都会被重视。

【问题讨论】:

  • 您是否为这个用例提供了解决方案?我正在寻找完全相同的用例的解决方案。

标签: flutter dart google-api youtube-api google-oauth


【解决方案1】:

您可能想看看这个documentation

它提供了一个使用 Python 将视频上传到 YouTube 的代码示例。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-26
    • 2017-12-10
    • 2012-12-20
    • 2021-06-15
    • 2014-11-19
    • 2014-11-24
    • 2013-12-18
    • 1970-01-01
    相关资源
    最近更新 更多