Flutter语言 - Android意图过滤器与Google Oauth



我已经用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中添加了intent-filter:

<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>

当登录功能运行时,google告诉我:"Custom schemes cannot have authority"

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

我改变Android凭据到Web应用程序,我可以添加自定义重定向uri。我在Web凭证中添加了一个假地址:"http://my.test.app"这个凭证给我一个client_id和一个secret。

修改sign_in函数:

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'
]);

成功:谷歌登录是工作和重定向到http://my.test.app,但过程卡住。浏览器提示我:"无法访问该站点"。安卓应用不会拦截响应。为什么? ?

我不认为我的假网站应该存在,不是吗?我认为重定向uri在查询参数中获得授权码,我的应用程序拦截它,oauth2_client包处理响应。

为什么把它卡在浏览器里?

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

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

在我的例子中:

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

最新更新