【问题标题】:Flutter - Android intent-filter with Google OauthFlutter - 带有 Google Oauth 的 Android 意图过滤器
【发布时间】:2021-03-07 20:31:30
【问题描述】:

我已经使用 Google Oauth 构建了一个 Flutter 应用程序,但在使用 intent-filter 时遇到了障碍。

我使用https://pub.dev/packages/oauth2_client 包登录。

我在 Google Api 控制台中创建了一个 Android 凭据,它只给了我一个 client_id(在本例中为:'abc')。

我的登录功能是:

GoogleOAuth2Client client = GoogleOAuth2Client(
                                customUriScheme: "my.test.app",
                                redirectUri: "my.test.app://oauth2redirect");

OAuth2Helper oauth2Helper = OAuth2Helper(client,
                                grantType: OAuth2Helper.AUTHORIZATION_CODE,
                                clientId: 'abc',
                                scopes: [
                                  'https://www.googleapis.com/auth/drive.readonly'
                                ]);

var resp = await oauth2Helper.get('https://www.googleapis.com/drive/v3/files');

我在 main/AndrodiManifest.xml 中添加了意图过滤器:

<activity android:name="com.linusu.flutter_web_auth.CallbackActivity" >
      <intent-filter android:label="flutter_web_auth">
          <action android:name="android.intent.action.VIEW" />
          <category android:name="android.intent.category.DEFAULT" />
          <category android:name="android.intent.category.BROWSABLE" />
          <data android:scheme="my.test.app" />
      </intent-filter>
  </activity>

当登录功能运行时,谷歌说我:“自定义方案没有权限”。

好的...我尝试使用“http://my.test.app/oauth2redirect”-> 重定向 uri 不匹配。 我从 API 控制台下载了 JSON,我看到我的 Android 凭据只有 2 个 redirect_uri:“urn:ietf:wg:oauth:2.0:oob”、“http://localhost”。这是从哪里来的,我可以在哪里添加更多重定向 uri?

我将 Android 凭据更改为可以添加自定义重定向 uri 的 Web 应用程序。 我在 Web 凭据中添加了一个虚假地址:“http://my.test.app”。这个凭证给了我一个 client_id 和一个秘密。

修改了登录功能:

GoogleOAuth2Client client = GoogleOAuth2Client(
                                customUriScheme: "my.test.app",
                                redirectUri: "http://my.test.app");

OAuth2Helper oauth2Helper = OAuth2Helper(client,
                                grantType: OAuth2Helper.AUTHORIZATION_CODE,
                                clientId: 'abc2',
                                clientSecret: 'acb3',
                                scopes: [
                                  'https://www.googleapis.com/auth/drive.readonly'
                                ]);

成功:Google 登录正在工作并重定向到 http://my.test.app,但程序卡住了。浏览器告诉我:“无法访问该站点”并且 Android 应用程序不会拦截响应。为什么??

我认为我的假网站不应该存在,不是吗??

我认为重定向 uri 在查询参数中获得了授权码,我的应用程序拦截了它并且 oauth2_client 包处理响应。

为什么会卡在浏览器中?

这是一个意图问题吗?我没有配置好东西?

【问题讨论】:

    标签: android flutter oauth-2.0 google-oauth


    【解决方案1】:

    我找到了解决方案:我必须重定向到客户端 ID 的反向 DNS 概念,并且必须使用 Android 凭据(仅使用 client_id)

    就我而言:

    android:scheme="com.googleusercontent.apps.XXXXXXXXXXXX-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
    
    customUriScheme="com.googleusercontent.apps.XXXXXXXXXXXX-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
    redirectUri: "com.googleusercontent.apps.XXXXXXXXXXXX-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX:/"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-10
      • 1970-01-01
      • 2011-02-09
      • 2015-05-14
      • 1970-01-01
      • 1970-01-01
      • 2012-02-22
      • 1970-01-01
      相关资源
      最近更新 更多