Access Youtube data API through Flutter app with OAuth2

白昼怎懂夜的黑 提交于 2020-08-10 18:49:51

问题


My Flutter app is using Firebase for Google Auth and Database and it needs to communicate with the Youtube Data Api v.3, in order to upload videos on the user's behalf. The libraries that seemed most functional are googleapis: ^0.55.0 and googleapis_auth: ^0.2.12, but there's a problem with the redirect url, in that they don't support it.
Implementing the code as in the [provided example][1](installed/console app) results in the following error in the console after clicking on Allow in the consent screen:

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

In the emulator I get a http error 500 (localhost unable to handle the request), but looking at the return URI, it seems like it containes the authorization code. Any thoughts on how I might get back the authorization code from the uri into the app? Right now I'm manually copying the auth url into a browser in order to get the consent screen, so I'm looking for an implementation that handles that. Here's my current code:

        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");
        }

Any input will be valued.


回答1:


You might want to have a look at this documentation.

It provides an code example to upload a video to YouTube using Python.



来源:https://stackoverflow.com/questions/63003853/access-youtube-data-api-through-flutter-app-with-oauth2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!