【问题标题】:Android redirect uriAndroid 重定向 uri
【发布时间】:2016-02-12 23:55:59
【问题描述】:

我正在使用 instagram API,但我不明白我应该为重定向 api 输入什么。目前我只是输入https://127.0.0.1

但我真的不明白。此外,一旦我得到允许/取消屏幕并按下允许,它就会给我一个错误,说我不能去那个地址,但我也可以看到附加在地址上的授权码。那么如何从我的重定向 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
            }
        }
    }

这是我处理意图过滤器的清单 sn-p:

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

【问题讨论】:

    标签: android redirect instagram-api


    【解决方案1】:

    您必须在浏览器视图上设置一个事件侦听器并检查 URL 参数中的 code 如果 URL 等于 redirect_uri,然后使用 codeclient_secret 向身份验证 URL 发出 POST 请求如 Instagram 身份验证中所述

    类似这样的:

    webView.setWebViewClient(new WebViewClient() {
       public void onPageFinished(WebView view, String url) {
            String code = url.getQueryParameter("code");
            // ...
        }
    });
    

    【讨论】:

    • 具体是怎么做的?我在下面发布我的简历以及我的清单 sn-ps 供您查看。
    • 我的重定向 uri 是什么?当我注册我的应用程序时,我可以放 google.com 吗?
    • 此外,我刚刚注册了另一个应用程序并将我自己的网站作为重定向 uri。果然这次它使用 code 参数重定向回 mywebsite 。唯一的问题是我不想访问我的网站,我希望它回到应用程序中。
    • 我还更改了 android:host="redirecturi" android:scheme="your" /> 我将您更改为 http 并将 redirecturi 更改为地址栏中的那个,但它仍然不会回到我的应用
    • 最后....通过剪切试验和错误。我的重定向 uri 在我的清单中是这个 = 127.0.0.1 我编辑它看起来像这样: 当我在授权后这样做时,我得到了重定向到127.0.0.1/?code 我收到网页不可用错误。但是,当我单击该链接时,它提示我使用我的登录活动或浏览器打开。如果我点击登录活动,我终于找到了回到应用程序的方法。但。为什么要这样做?为什么它不回到我的应用程序中,这样我就可以提取代码并发出我的发布请求
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-09
    • 2013-05-21
    • 1970-01-01
    • 2013-11-19
    • 2017-05-04
    相关资源
    最近更新 更多