使用OAuth2通过Flutter应用程序访问Youtube数据API



My Flutter应用程序正在使用Firebase进行谷歌认证和数据库,它需要与Youtube Data Api v.3进行通信,以便代表用户上传视频。功能最强大的库是googleapis: ^0.55.0googleapis_auth: ^0.2.12,但重定向url存在问题,因为它们不支持它

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

任何输入都将被赋值。

您可能需要看一下此文档。

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

最新更新