【问题标题】:Unhandled Exception: NoSuchMethodError: The getter 'authentication' was called on null [GoogleAPI + YoutubeAPI + Firebase + Flutter]未处理的异常:NoSuchMethodError:在 null [GoogleAPI + YoutubeAPI + Firebase + Flutter] 上调用了 getter 'authentication'
【发布时间】:2021-09-07 08:31:05
【问题描述】:

我正面临这个问题,看起来我的 auth var 为空,但我没有发现任何问题。错误:

E/flutter (24944): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: NoSuchMethodError: The getter 'authentication' was called on null.
E/flutter (24944): Receiver: null
E/flutter (24944): Tried calling: authentication
E/flutter (24944): #0      Object.noSuchMethod (dart:core-patch/object_patch.dart:54:5)

我的代码(只是有问题的函数):

  addPlayListItem(String videoID, String playlistID) async {
    final String apiUrl =
        "https://www.googleapis.com/youtube/v3/playlistItems?part=snippet";
    final FirebaseAuth _auth = FirebaseAuth.instance;
    final GoogleSignIn googleSignIn = GoogleSignIn(
      scopes: [
        'email',
        'profile',
        'https://www.googleapis.com/auth/youtube',
      ],
    );

    final GoogleSignInAccount googleSignInAccount = await googleSignIn.signInSilently();
    final GoogleSignInAuthentication googleSignInAuthentication =
    await googleSignInAccount.authentication;

    final AuthCredential credential = GoogleAuthProvider.getCredential(
        idToken: googleSignInAuthentication.idToken,
        accessToken: googleSignInAuthentication.accessToken);

    final AuthResult authResult = await _auth.signInWithCredential(credential);
    final FirebaseUser user = authResult.user;

    assert(!user.isAnonymous);
    assert(await user.getIdToken() != null);

    final FirebaseUser currentUser = await _auth.currentUser();
    assert(currentUser.uid == user.uid);

    void signOutGoogle() async {
      await googleSignIn.signOut();
    }

    print("token  " +
        googleSignInAuthentication.accessToken); // accessToken not idToken

    final response = await http.post(
      apiUrl,
      headers: {
        "Authorization": "Bearer ${googleSignInAuthentication.accessToken}",
        // Use access token and add a space after the 'Bearer
        "Accept": "application/json",
      },
      body: jsonEncode({
        "snippet": {
          "playlistId": "" + playlistID,
          "resourceId": {"kind": "youtube#video", "videoId": "" + videoID}
        }
      }),
    );
    if (response.statusCode == 200) {
      print("Song was added to playlist");
    } else {
      print(response.body);
      print("Failture song wasn't added to playlist");
    }
  }

这是我在 iOS 设备上尝试使用该功能时得到的错误代码。也许这会提供更多信息。

2021-09-11 11:58:53.310702+0200 Runner[6447:55089] flutter: {
  "error": {
    "code": 403,
    "message": "Request had insufficient authentication scopes.",
    "errors": [
      {
        "message": "Insufficient Permission",
        "domain": "global",
        "reason": "insufficientPermissions"
      }
    ],
    "status": "PERMISSION_DENIED"
  }
}
2021-09-11 11:58:53.310854+0200 Runner[6447:55089] flutter: Failture song wasn't added to playlist

我好像遇到了身份验证问题,但我不知道从哪里开始。

更新安卓: 干净的重建后,我得到了这个错误代码


E/flutter ( 3537): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: PlatformException(sign_in_failed, com.google.android.gms.common.api.ApiException: 12500: , null, null)
E/flutter ( 3537): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:597:7)
E/flutter ( 3537): #1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:158:18)
E/flutter ( 3537): <asynchronous suspension>
E/flutter ( 3537): #2      MethodChannel.invokeMapMethod (package:flutter/src/services/platform_channel.dart:358:43)
E/flutter ( 3537): <asynchronous suspension>
E/flutter ( 3537): #3      GoogleSignIn._callMethod (package:google_sign_in/google_sign_in.dart:233:30)
E/flutter ( 3537): <asynchronous suspension>
E/flutter ( 3537): #4      GoogleSignIn.signIn.isCanceled (package:google_sign_in/google_sign_in.dart)
E/flutter ( 3537): <asynchronous suspension>
E/flutter ( 3537): 

【问题讨论】:

    标签: firebase flutter google-api youtube-api


    【解决方案1】:

    来自signInSilently 方法的文档:

    尝试登录之前已通过身份验证的用户,而无需 互动。

    返回的 Future 解析为 GoogleSignInAccount 的实例 登录成功或null,如果之前没有经过身份验证 用户。使用signIn方法触发交互式登录过程。

    所以您将googleSignInAccount 设为null,因为之前没有登录用户。

    您可以添加检查以查看googleSignInAccount 是否为null,如果为真则使用signIn

    所以你可以更新这个:

    ...
    final GoogleSignInAccount googleSignInAccount = await googleSignIn.signInSilently();
    ...
    

    到这里:

    ...
    GoogleSignInAccount googleSignInAccount = await googleSignIn.signInSilently();
    
    if (googleSignAccount == null) {
      googleSignInAccount = await googleSignIn.signIn();
    }
    ...
    

    【讨论】:

    • 我添加了这个,好吧,当我尝试调用这个函数时,现在只是打开一个窗口并继续加载。
    • 您应该使用try-catch 包装您的代码并在catch 块中打印错误。可能有错误阻止它继续。
    • 好吧,尽管它仍然与我的问题中提到的错误相同
    • 你能澄清一下吗?它现在会打开窗口吗?上面会显示什么?
    • 好吧,它是来自谷歌的通知窗口,我猜你必须在哪里接受权限,但它只是不断加载。
    猜你喜欢
    • 2020-09-28
    • 2022-01-16
    • 2021-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-09
    • 2020-11-03
    • 2021-06-23
    相关资源
    最近更新 更多