2011-04-05 30 views
6

我正在爲我的研究的Android應用程序,而我通過OAuth(路標庫)合作,以獲得從Web服務,這是訪問用戶數據也是開發過程的一部分。我能夠通過OAuth的常見步驟進行流程,並且使用Uri(用於回調應用程序),並且可以進入調用設備瀏覽器的步驟,選擇驗證我的應用程序,然後下一步是SUPPOSED要返回給應用程序瀏覽器重定向....Android開發人員 - 回調URL不工作...(0_o)

相反,我得到讀取類似的錯誤「您沒有權限打開:

appSchema:// APPNAME authorizationSensitiveInfo ... 「'後面的附屬物' '?'是 組oauth_token和oauth_verifier從 服務(我們可以假設所有步驟 直到重定向是 「正確」)。

可能存在的問題在於appSchema://appName部分。根據我的理解,這是重定向URL,它告訴Uri使用手機的瀏覽器來定位我的應用程序並調用onResume()方法。 appSchema://appName的值來自哪裏(在清單中定義,如果在那裏?)。

爲什麼有權限的問題?我必須爲我的Uri設置訪問我的應用程序的權限嗎?我迷路了...如果你需要代碼片段來幫助我請回復,我沒有包含任何代碼,因爲這更像是一個我剛剛錯過的概念...我現在不在我的機器上,但我可以提供如果這會使事情變得更容易理解。真的打我的這裏頭......

輸入反應中一個偉大的答案下面是我處理我在恢復

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檢查消息,並從服務器...

回答

10

爲了回調URI才能正常工作,你需要添加類似下面的一個意圖過濾器您在活動清單要在使用它

<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,你應該使用類似下面的東西:如果不使用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 
} 

或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?」

+0

我會試一試,讓你知道這是否有效。我的主要活動中有明顯的意圖過濾爵士樂,我將它移動到使用Oauth並添加'?'的活動中。到我的回撥網址。謝謝! – 2011-04-05 18:50:07

+0

您是通過ACTION_VIEW類型的意圖啓動瀏覽器嗎?您可能遇到的一個可能的煩惱是,雖然瀏覽器將重定向回您的應用程序,但它不會自動關閉。不知道這是否會阻礙你想要完成的事情。 – ootinii 2011-04-05 20:19:15

+0

我正在使用ACTION_VIEW,我有一個問題是如何確定我是否使用單個實例/ singleTop,是否就像我沒有看到這些單詞一樣簡單(我確定從不會手動調用),那麼我可以使用onResume而不是onNewIntent()。我將意圖過濾器包含在清單中的活動中,但現在我的應用程序部隊關閉了。我正在嘗試執行的操作是將回調中的oauth信息保存到SharedPreferences文件,以便我的應用程序的其他活動可以使用該文件。但不是權限的消息,它只是強制關閉:( – 2011-04-05 22:23:28