Youtube Api PlaylistItems 插入错误 Flutter
Youtube Api PlaylistItems insert error Flutter
问题是服务器显示 401 未授权。
有人知道我如何编写将视频插入播放列表的函数吗?
final String apiUrl = "https://youtube.googleapis.com/youtube/v3/playlistItems?key=" +
ApiKey.API_KEY;
final GoogleSignInAccount googleUser = await GoogleSignIn().signInSilently();
final GoogleSignInAuthentication googleAuth = await googleUser.authentication;
final credential = GoogleAuthProvider.getCredential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.serverAuthCode,
);
print("token "+googleAuth.idToken);
final response = await http.post(apiUrl, headers: {
"Authorization": "Bearer"+ googleAuth.idToken,
"Accept": "application/json",
},
);
if (response.statusCode == 200){
print("Song was added to playlist");
}
else{
print(response.statusCode);
print("Failture song wasn't added to playlist");
}
}
这就是在 YouTube-Api 测试中对我有用的方法 window
POST https://youtube.googleapis.com/youtube/v3/playlistItems?part=snippet&key=[YOUR_API_KEY] HTTP/1.1
Authorization: Bearer [YOUR_ACCESS_TOKEN]
Accept: application/json
Content-Type: application/json
{
"snippet": {
"playlistId": "PLu55WjdIfjnZznsIY0waSdBFMdmle9Xu2",
"position": 0,
"resourceId": {
"kind": "youtube#video",
"videoId": "M7FIvfx5J10"
}
}
}
但现在我的问题是,我如何将其转换为工作函数
出现错误
E/flutter ( 8397): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: type '_InternalLinkedHashMap<String, Object>' is not a subtype of type 'String' in type cast
E/flutter ( 8397): #0 CastMap.forEach.<anonymous closure> (dart:_internal/cast.dart:288:25)
E/flutter ( 8397): #1 _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:397:8)
E/flutter ( 8397): #2 CastMap.forEach (dart:_internal/cast.dart:287:13)
E/flutter ( 8397): #3 mapToQuery (package:http/src/utils.dart:17:7)
E/flutter ( 8397): #4 Request.bodyFields= (package:http/src/request.dart:137:12)
E/flutter ( 8397): #5 BaseClient._sendUnstreamed (package:http/src/base_client.dart:85:17)
E/flutter ( 8397): #6 BaseClient.post (package:http/src/base_client.dart:32:7)
E/flutter ( 8397): #7 post.<anonymous closure> (package:http/http.dart:70:16)
E/flutter ( 8397): #8 _withClient (package:http/http.dart:166:20)
E/flutter ( 8397): #9 post (package:http/http.dart:69:5)
E/flutter ( 8397): #10 AdminAddRequestToPlaylist.addVideoToPlaylist (package:flutter_app/screens/admin/conponents/AdminAddRequestsToPlaylist.dart:125:28)
E/flutter ( 8397): <asynchronous suspension>
E/flutter ( 8397):
您需要使用 accessToken
而不是 idToken
。试试下面的代码
addPlayListItem() async {
final String apiUrl = "https://www.googleapis.com/youtube/v3/playlistItems";
final GoogleSignInAccount googleUser = await GoogleSignIn()
.signInSilently();
final GoogleSignInAuthentication googleAuth = await googleUser
.authentication;
final credential = GoogleAuthProvider.getCredential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.serverAuthCode,
);
print("token " + googleAuth.accessToken); // accessToken not idToken
final response = await http.post(
apiUrl,
headers: {
"Authorization": "Bearer ${googleAuth.accessToken}", // Use access token and add a space after the 'Bearer
"Accept": "application/json",
},
body: jsonEncode({
"playlistId": "PLu55WjdIfjnZznsIY0waSdBFMdmle9Xu2",
"position": 0,
"resourceId": {
"kind": "youtube#video",
"videoId": "M7FIvfx5J10"
}
}),
);
if (response.statusCode == 200) {
print("Song was added to playlist");
}
else {
print(response.statusCode);
print("Failture song wasn't added to playlist");
}
}
问题是服务器显示 401 未授权。 有人知道我如何编写将视频插入播放列表的函数吗?
final String apiUrl = "https://youtube.googleapis.com/youtube/v3/playlistItems?key=" +
ApiKey.API_KEY;
final GoogleSignInAccount googleUser = await GoogleSignIn().signInSilently();
final GoogleSignInAuthentication googleAuth = await googleUser.authentication;
final credential = GoogleAuthProvider.getCredential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.serverAuthCode,
);
print("token "+googleAuth.idToken);
final response = await http.post(apiUrl, headers: {
"Authorization": "Bearer"+ googleAuth.idToken,
"Accept": "application/json",
},
);
if (response.statusCode == 200){
print("Song was added to playlist");
}
else{
print(response.statusCode);
print("Failture song wasn't added to playlist");
}
}
这就是在 YouTube-Api 测试中对我有用的方法 window
POST https://youtube.googleapis.com/youtube/v3/playlistItems?part=snippet&key=[YOUR_API_KEY] HTTP/1.1
Authorization: Bearer [YOUR_ACCESS_TOKEN]
Accept: application/json
Content-Type: application/json
{
"snippet": {
"playlistId": "PLu55WjdIfjnZznsIY0waSdBFMdmle9Xu2",
"position": 0,
"resourceId": {
"kind": "youtube#video",
"videoId": "M7FIvfx5J10"
}
}
}
但现在我的问题是,我如何将其转换为工作函数
出现错误
E/flutter ( 8397): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: type '_InternalLinkedHashMap<String, Object>' is not a subtype of type 'String' in type cast
E/flutter ( 8397): #0 CastMap.forEach.<anonymous closure> (dart:_internal/cast.dart:288:25)
E/flutter ( 8397): #1 _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:397:8)
E/flutter ( 8397): #2 CastMap.forEach (dart:_internal/cast.dart:287:13)
E/flutter ( 8397): #3 mapToQuery (package:http/src/utils.dart:17:7)
E/flutter ( 8397): #4 Request.bodyFields= (package:http/src/request.dart:137:12)
E/flutter ( 8397): #5 BaseClient._sendUnstreamed (package:http/src/base_client.dart:85:17)
E/flutter ( 8397): #6 BaseClient.post (package:http/src/base_client.dart:32:7)
E/flutter ( 8397): #7 post.<anonymous closure> (package:http/http.dart:70:16)
E/flutter ( 8397): #8 _withClient (package:http/http.dart:166:20)
E/flutter ( 8397): #9 post (package:http/http.dart:69:5)
E/flutter ( 8397): #10 AdminAddRequestToPlaylist.addVideoToPlaylist (package:flutter_app/screens/admin/conponents/AdminAddRequestsToPlaylist.dart:125:28)
E/flutter ( 8397): <asynchronous suspension>
E/flutter ( 8397):
您需要使用 accessToken
而不是 idToken
。试试下面的代码
addPlayListItem() async {
final String apiUrl = "https://www.googleapis.com/youtube/v3/playlistItems";
final GoogleSignInAccount googleUser = await GoogleSignIn()
.signInSilently();
final GoogleSignInAuthentication googleAuth = await googleUser
.authentication;
final credential = GoogleAuthProvider.getCredential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.serverAuthCode,
);
print("token " + googleAuth.accessToken); // accessToken not idToken
final response = await http.post(
apiUrl,
headers: {
"Authorization": "Bearer ${googleAuth.accessToken}", // Use access token and add a space after the 'Bearer
"Accept": "application/json",
},
body: jsonEncode({
"playlistId": "PLu55WjdIfjnZznsIY0waSdBFMdmle9Xu2",
"position": 0,
"resourceId": {
"kind": "youtube#video",
"videoId": "M7FIvfx5J10"
}
}),
);
if (response.statusCode == 200) {
print("Song was added to playlist");
}
else {
print(response.statusCode);
print("Failture song wasn't added to playlist");
}
}