首页 > 解决方案 > Github Flutter 登录

问题描述

Github API指定需要重定向 url 才能获得授权的登录结果。但是,如果我正在开发一个不使用 webviews 的非混合颤振应用程序。在提示用户使用URL Launcher之类的登录后,我将如何重定向用户以返回应用程序?

标签: flutter

解决方案


您可以使用包https://pub.dev/packages/simple_auth

代码片段

final simpleAuth.GithubApi githubApi = new simpleAuth.GithubApi(
      "github", "clientId", "clientSecret", "redirect:/",
      scopes: [
        "user",
        "repo",
        "public_repo",
      ]);

void login(simpleAuth.AuthenticatedApi api) async {
    try {
      var success = await api.authenticate();
      showMessage("Logged in success: $success");
    } catch (e) {
      showError(e);
    }
  }
... 
ListTile(
            leading: Icon(Icons.launch),
            title: Text('Login'),
            onTap: () {
              login(githubApi);
            },
          ),
ListTile(
            leading: Icon(Icons.delete),
            title: Text('Logout'),
            onTap: () {
              logout(githubApi);
            },
          ),

完整示例代码 https://github.com/Clancey/simple_auth/blob/master/simple_auth_flutter_example/lib/main.dart


推荐阅读