我正在构建一个使用 instagram API 的 android 应用程序,我想知道是否有一种解决方案可以避免构建 api(重定向 uri)来处理来自 instragram 身份验证端点的重定向,以便为 android 客户端检索 token 。
最佳答案
我想你可以用他们的 Client side (implicit) authentication 来实现.检查文章末尾的部分。
Once the user has authenticated and then authorized your application, Instagram redirects them to your redirect_uri with the access_token in the url fragment. It will look like this:
http://your-redirect-uri#access_token=ACCESS-TOKEN
Simply grab the access_token off the URL fragment and you’re good to go. If the user chooses not to authorize your application, you’ll receive the same error response as in the explicit flow.
关键部分是您必须定义重定向uri,但在这种情况下它必须无效。例如,如果您使用 www.google.com
作为重定向 uri,在身份验证后用户将被重定向到
此时,您必须不让用户继续前进。相反,捕获重定向并解析 URI 字符串,以便您可以获得访问 token 。
如果您的导航发生在 WebView 内部,这可能发生在 shouldOverrideUrlLoading()
回调(来自 WebViewClient)中:
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("http://www.google.com") { // redirect uri!
String accessToken = ... // get token from url
return true; // don’t load the page
}
return false;
}
这会有些痛苦,例如错误处理,但是嘿,如果他们有客户端身份验证,它应该也适用于您。
关于android - 在 android 中集成 instagram 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41007386/