LocalServer和Flutter应用程序之间的连接

时间:2019-11-27 12:27:03

标签: flutter oauth-2.0

我有一个与Flutter应用开发有关的问题。 问题是,“可以在没有Firebase的情况下实现OAuth2吗?

基本上,我想将我的Android应用程序与自己的localserver链接以对用户进行身份验证。每个人都首先向Firebase注册该应用程序,然后再继续。 如果有任何解决方案,请与我分享。

1 个答案:

答案 0 :(得分:0)

您可以使用oauth2客户端软件包https://pub.dev/packages/oauth2
Github https://github.com/dart-lang/oauth2

客户凭证授予代码段

// This URL is an endpoint that's provided by the authorization server. It's
// usually included in the server's documentation of its OAuth2 API.
final authorizationEndpoint =
    Uri.parse("http://example.com/oauth2/authorization");

// The OAuth2 specification expects a client's identifier and secret
// to be sent when using the client credentials grant.
//
// Because the client credentials grant is not inherently associated with a user,
// it is up to the server in question whether the returned token allows limited
// API access.
//
// Either way, you must provide both a client identifier and a client secret:
final identifier = "my client identifier";
final secret = "my client secret";

// Calling the top-level `clientCredentialsGrant` function will return a
// [Client] instead.
var client = await oauth2.clientCredentialsGrant(
    authorizationEndpoint, identifier, secret);

// With an authenticated client, you can make requests, and the `Bearer` token
// returned by the server during the client credentials grant will be attached
// to any request you make.
var response = await client.read("https://example.com/api/some_resource.json");

// You can save the client's credentials, which consists of an access token, and
// potentially a refresh token and expiry date, to a file. This way, subsequent runs
// do not need to reauthenticate, and you can avoid saving the client identifier and
// secret.
await credentialsFile.writeAsString(client.credentials.toJson());