Android 开发 - 回调 URL 无效... (0_o)

标签 android oauth callback android-manifest signpost

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

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

appSchema://appName?authorizationSensitiveInfo..." the appendages after the '?' are the oauth_token and oauth_verifier from the service (we can assume all steps up until the redirection are "correct").

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

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

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

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 检查进出服务器的消息...

最佳答案

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

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

现在,如果您的 Activity 使用的是 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?"

关于Android 开发 - 回调 URL 无效... (0_o),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5545803/

相关文章:

android - 从 Android 应用程序中排除隐藏的 Google AdMob 广告依赖项

php - 实时检索推文并发送带有推文文本的电子邮件

c++ - 在函数指针之间转换

ios - handleTurnEventForMatch :didBecomeActive: callbacks only arriving some of the time

java - Appium - 通过 Xpath 查找元素

android - 重构更改项目和包名称后 map 不显示

authentication - github(twitter、stripe & co)是否使用 oauth 进行自己的登录?

java - Android Facebook 登录回调不起作用

android - 从任何移动平台上的前置摄像头捕获视频

oauth - LinkedIn OAuth2 撤销/使 token 无效