Android 重定向
Android redirect uri
我正在使用 instagram API,但我不明白我应该为重定向输入什么 api。目前我只是输入 https://127.0.0.1
但是我不是很懂。此外,一旦我看到 allow/cancel 屏幕并按下允许,它就会给我一个错误,提示我无法前往该地址,但我还可以看到该地址上附加的授权码。那么如何从我的重定向 uri 重定向回来呢?我如何告诉 android 在用户点击允许返回应用程序后使用代码进行进一步的身份验证?
我确定您会说一些类似制作我自己的自定义方案/意图过滤器等的内容,但请多多支持我的新功能,我不明白,我确实对它们进行了研究。
我的简历方法如下
@Override
protected void onResume() {
super.onResume();
// the intent filter defined in AndroidManifest will handle the return from ACTION_VIEW intent
Uri uri = getIntent().getData();
if (uri != null && uri.toString().startsWith(redirectUri)) {
// use the parameter your API exposes for the code (mostly it's "code")
String code = uri.getQueryParameter("code");
if (code != null) {
// get access token
LoginService loginService =
ServiceGenerator.createService(LoginService.class, clientId, clientSecret);
AccessToken accessToken = loginService.getAccessToken(code, "authorization_code");
} else if (uri.getQueryParameter("error") != null) {
// show an error message here
}
}
}
这是我处理意图过滤器的清单片段:
<activity
android:name=".LoginActivity"
android:label="@string/title_activity_login" >
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="redirecturi"
android:scheme="your" />
</intent-filter>
</activity>
您必须在浏览器视图上设置事件侦听器并检查 URL 参数中的 code
如果 URL 等于 redirect_uri,然后使用 code
和 client_secret
向 auth URL 发出 POST 请求,如 Instagram 身份验证
中所述
像这样:
webView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
String code = url.getQueryParameter("code");
// ...
}
});
我正在使用 instagram API,但我不明白我应该为重定向输入什么 api。目前我只是输入 https://127.0.0.1
但是我不是很懂。此外,一旦我看到 allow/cancel 屏幕并按下允许,它就会给我一个错误,提示我无法前往该地址,但我还可以看到该地址上附加的授权码。那么如何从我的重定向 uri 重定向回来呢?我如何告诉 android 在用户点击允许返回应用程序后使用代码进行进一步的身份验证?
我确定您会说一些类似制作我自己的自定义方案/意图过滤器等的内容,但请多多支持我的新功能,我不明白,我确实对它们进行了研究。
我的简历方法如下
@Override
protected void onResume() {
super.onResume();
// the intent filter defined in AndroidManifest will handle the return from ACTION_VIEW intent
Uri uri = getIntent().getData();
if (uri != null && uri.toString().startsWith(redirectUri)) {
// use the parameter your API exposes for the code (mostly it's "code")
String code = uri.getQueryParameter("code");
if (code != null) {
// get access token
LoginService loginService =
ServiceGenerator.createService(LoginService.class, clientId, clientSecret);
AccessToken accessToken = loginService.getAccessToken(code, "authorization_code");
} else if (uri.getQueryParameter("error") != null) {
// show an error message here
}
}
}
这是我处理意图过滤器的清单片段:
<activity
android:name=".LoginActivity"
android:label="@string/title_activity_login" >
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="redirecturi"
android:scheme="your" />
</intent-filter>
</activity>
您必须在浏览器视图上设置事件侦听器并检查 URL 参数中的 code
如果 URL 等于 redirect_uri,然后使用 code
和 client_secret
向 auth URL 发出 POST 请求,如 Instagram 身份验证
像这样:
webView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
String code = url.getQueryParameter("code");
// ...
}
});