【问题标题】:Android Dev - Callback URL not working... (0_o)Android 开发 - 回调 URL 不起作用... (0_o)
【发布时间】:2011-07-29 14:00:03
【问题描述】:

我正在为我的研究开发一个 android 应用程序,并且我正在使用 OAuth(路标库)从 Web 服务中获取对用户数据的访问权限,这也是开发过程的一部分。我能够完成 OAuth 的常见步骤,并且我使用 Uri(用于回调应用程序),并且可以进入调用设备浏览器的步骤,选择验证我的应用程序,下一步是 SUPPOSED将浏览器重定向回应用程序....

相反,我收到一条错误消息,内容类似于“您没有打开权限:

appSchema://appName?authorizationSensitiveInfo..." '?' 之后的附属物是 oauth_token 和 oauth_verifier 来自 服务(我们可以假设所有步骤 直到重定向 “正确”)。

appSchema://appName 部分可能存在问题。据我了解,这是重定向 URL,它告诉 Uri 使用手机的浏览器来定位我的应用程序并调用 onResume() 方法。 appSchema://appName 的值来自哪里(在清单中定义?如果是在哪里?)。

为什么会出现权限问题?我必须为我的 Uri 设置访问我的应用程序的权限吗?我迷路了...如果您需要代码 sn-ps 来帮助我,请回复,我没有包含任何代码,因为这更像是我刚刚错过的一个概念...我现在不在我的机器上,但我如果这会使事情更容易理解,可以提供代码。真的是在这里打我的头......

为了回应一个很好的答案,我将如何处理我的简历

protected void onResume() {
    super.onResume();       
    Uri uri = this.getIntent().getData();
    if (uri != null && uri.toString().startsWith(CALLBACK_URL)) {
        Log.d("StepGreenM", uri.toString());
        String verifier = uri.getQueryParameter(OAuth.OAUTH_VERIFIER);
        Log.d("StepGreenM", verifier);
        try {

            provider.retrieveAccessToken(consumer, verifier);
            TOKEN = consumer.getToken();
            REQUEST_SECRET = consumer.getTokenSecret();

            Log.d("StepGreenM", TOKEN);
            Log.d("StepGreenM", REQUEST_SECRET);

        } catch (OAuthMessageSignerException e) {
            e.printStackTrace();
        } catch (OAuthNotAuthorizedException e) {
            e.printStackTrace();
        } catch (OAuthExpectationFailedException e) {
            e.printStackTrace();
        } catch (OAuthCommunicationException e) {
            e.printStackTrace();
        }
    }

    uri = getIntent().getData();
    if (uri != null && CALLBACK_URI.getScheme().equals(uri.getScheme())) {
        String token = settings.getString(HomeScreen.REQUEST_TOKEN, null);
        String secret = settings.getString(HomeScreen.REQUEST_SECRET, null);
        Intent i = new Intent(Intent.ACTION_VIEW); // Intent to go to the action view

        try {
            if(!(token == null || secret == null)) {
                consumer.setTokenWithSecret(token, secret);
            }
            String otoken = uri.getQueryParameter(OAuth.OAUTH_TOKEN);
            String verifier = uri.getQueryParameter(OAuth.OAUTH_VERIFIER);

            // We send out and save the request token, but the secret is not the same as the verifier
            // Apparently, the verifier is decoded to get the secret, which is then compared - crafty
            // This is a sanity check which should never fail - hence the assertion
            Assert.assertEquals(otoken, consumer.getToken());

            // This is the moment of truth - we could throw here
            provider.retrieveAccessToken(consumer, verifier);
            // Now we can retrieve the goodies
            token = consumer.getToken();
            secret = consumer.getTokenSecret();
            //Save it to a settings file
            HomeScreen.saveAuthInformation(settings, token, secret);
            // Clear the request stuff, now that we have the real thing
            HomeScreen.saveRequestInformation(settings, null, null);
            i.putExtra(USER_TOKEN, token);
            i.putExtra(CONSUMER_SECRET, secret);

            //GO TO APPLICATION

        } catch (OAuthMessageSignerException e) {
            e.printStackTrace();
        } catch (OAuthNotAuthorizedException e) {
            e.printStackTrace();
        } catch (OAuthExpectationFailedException e) {
            e.printStackTrace();
        } catch (OAuthCommunicationException e) {
            e.printStackTrace();
        } finally {
            startActivity(i); // we either authenticated and have the extras or not, but are going to the action view
            this.setContentView(R.layout.indivaction);
            finish();
        }
    }
}

不确定是什么真正让这个崩溃......但就像我说的那样,当调用这个方法时它会强制关闭。我知道它可以通过重定向,因为我使用 httpSniffer 检查进出服务器的消息...

【问题讨论】:

    标签: android oauth callback android-manifest signpost


    【解决方案1】:

    为了使回调 uri 正常工作,您需要在要在其中使用它的 Activity 中的清单中添加类似于以下内容的意图过滤器。

       <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:scheme="appSchema" android:host="appName"/> 
       </intent-filter>
    

    现在,如果您的活动正在使用 singleInstance/singleTask,您应该使用类似于以下内容的内容:

    @Override
    public void onNewIntent(Intent intent) {
    
        super.onNewIntent(intent);
        Uri uri = intent.getData();
        String oauthToken = uri.getQueryParameter("oauth_token");
        String oauthVerifier = uri.getQueryParameter("oauth_verifier");
    
        //...do what you need with the parameters
    }
    

    如果不使用 singleTask 或 singleInstance,你可以这样做

    @Override
    public void onResume() {
    
        super.onResume();
        Intent intent = getIntent();
        Uri uri = intent.getData();
        String oauthToken = uri.getQueryParameter("oauth_token");
        String oauthVerifier = uri.getQueryParameter("oauth_verifier");
    
        //...do what you need with the parameters
    }
    

    我相信这应该可行。

    另外,如果我没记错的话,你提供的回调 url 应该包含 ?,所以“appSchema://appName?”

    【讨论】:

    • 我会试一试,让你知道它是否有效。我的主要活动中有明显的意图过滤爵士乐,我将把它移到使用 Oauth 的活动并附加“?”到我的回调 URL。谢谢!
    • 您是否通过 ACTION_VIEW 类型的意图启动浏览器?您可能会遇到的一种烦恼是,尽管浏览器会重定向回您的应用程序,但它不会自动关闭。不知道这是否会阻碍你想要完成的事情。
    • 我正在使用 ACTION_VIEW,我有一个问题是我如何确定我是否在使用单实例/单顶,就像我没有看到这些词一样简单(我肯定从不调用要么手动)然后我可以使用onResume而不是onNewIntent()。我在清单中的活动中包含了意图过滤器,但现在我的应用程序强制关闭。我在恢复时尝试做的是将回调中的 oauth 信息保存到 SharedPreferences 文件中,以便我的应用程序的其他活动可以使用它。但不是权限消息,它只是强制关闭:(
    • 我的错,我的意思是 singleInstance/singleTask,但如果你没有在清单中为该活动设置 android:launchMode,它应该使用标准,这意味着你使用 onResume 是正确的反对 onNewIntent。当您完成浏览器后,回调是否会重新调用您的活动?
    • 在这些片段中是否必须分别使用 super.onResume() 和 super.onNewIntent()?
    猜你喜欢
    • 2011-10-06
    • 1970-01-01
    • 2012-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-23
    • 1970-01-01
    相关资源
    最近更新 更多