我想使用 Google OAuth2 获取访问令牌和刷新令牌。 我无法将重定向网址传递给 Android 应用程序。所以,我尝试使用网络应用程序。
到目前为止,这是我的代码。我无法弄清楚customUriScheme 和redirectUri 必须是什么。在 Azure 中,我使用重定向 uri 作为 com.example.calendar://oauth2redirect 访问了刷新令牌和访问令牌。 但 Google 不允许我将其添加到 webclient 中的重定向 URL。 但问题是 google webclient https 需要有效的重定向方案。
//Instantiate an OAuth2Client...
GoogleOAuth2Client client = GoogleOAuth2Client(
customUriScheme: 'my.test.app',
redirectUri: 'https://my.test.app/oauth2redirect',
);
var token = await client.getTokenWithAuthCodeFlow(
clientId: '',
clientSecret: '',
scopes: [
'https://www.googleapis.com/auth/calendar',
'https://www.googleapis.com/auth/calendar.readonly'
],
authCodeParams: {"prompt": "consent", "access_type": "offline"},
);
在 Android 清单中 我已在 Azure 中使用此方案来获取访问和刷新令牌以获取 Outlook 日历事件。 我已经对 Google OAuth 进行了相同的尝试,但没有成功。
<activity android:name="com.linusu.flutter_web_auth_2.CallbackActivity" android:exported="true">
<intent-filter android:label="flutter_web_auth_2">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="com.example.calendar" />
</intent-filter>
</activity>
//我也尝试过这个...
<activity android:name="com.linusu.flutter_web_auth_2.CallbackActivity">
<intent-filter android:label="flutter_web_auth_2" android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="https"
android:host="my.test.app"
android:pathPrefix="/oauth2redirect" />
</intent-filter>
</activity>
此后所有身份验证和权限授予均已完成。显示无法访问此站点页面。并且令牌为空。
谢谢您的帮助。
我已经通过直接使用 webview_flutter 包解决了这个问题。我通过注册 Web 应用程序使用了 Google 开发者控制台中的客户端 ID 和客户端密钥。当 Web 视图中发生重定向时,我查询了访问令牌和其他信息。 您可以参考Google OAuth 2.0 Doc