2013-07-31 54 views
1

對不起,我的英語不好,我會盡量簡單地解釋我的問題。 我正在嘗試製作一個應用程序,它可以與Yandex API一起使用。在他們的幫助頁面上,我讀過你應該從用戶登錄的應用程序啓動瀏覽器,然後通過註冊URI回調返回到應用程序。 我現在擁有的一切:如何從瀏覽器意向獲取授權令牌?

@Override 
public void onClick(View view) { 
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://oauth.yandex.ru/authorize?response_type=token&client_id=XXXXXXXXXXXXXXX")); 
startActivity(browserIntent); 
} 

這將啓動瀏覽器,並重定向到授權頁面。輸入登錄密碼後,我會自動返回到我的應用程序。這裏的AndroidManifest:

<activity 
      android:name="ru.mastergroosha.yaruclient.Main" 
      android:label="@string/app_name"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN"/> 
      <category android:name="android.intent.category.LAUNCHER"/> 
     </intent-filter> 
    </activity> 
    <activity 
      android:name="ru.mastergroosha.yaruclient.Window" 
      android:label="Window"> 
     <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="yaruapp" 
        android:host="token"/> 
     </intent-filter> 
    </activity> 

當我輸入的登錄密碼,我重定向到頁面像 「yaruapp://令牌#=的access_token YYYYYYYYYYYYYYY & token_type =代碼......」等等。但是我沒有看到這個頁面,因爲我們立即重定向迴應用程序。

的問題是:我怎麼能得到並提取這一部分:YYYYYYYYYYYYYY?

我爲如此noobish非常抱歉,希望你能幫助我。在此先感謝

回答

4

可以在onNewIntent得到URI。只需在活動中覆蓋它即可。你可以訪問令牌像下面這樣:

@Override 
protected void onNewIntent (Intent intent){ 
    Uri uri = intent.getData(); 
    if (uri!=null){ 
    String mainPart = uri.toString().split("#")[1]; 
    String[] arguments = mainPart.split["&"]; 
    String argument = arguments[0]; 
    String token = argument.split("=")[1]; 
}