2013-07-31 52 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]; 
}