我怎样才能从浏览器的意图授权令牌? [英] How can I get authorization token from browser intent?

查看:431
本文介绍了我怎样才能从浏览器的意图授权令牌?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,我英文不好,我会试着解释我的问题那么简单,我可以。
我试图让一个应用程序,它与Yandex的API的工作。在他们的帮助页面,我读过,你应该从应用,在那里用户登录启动浏览器,然后通过注册URI回调返回到应用程序。
我现在有什么:

Sorry for my bad English, I'll try to explain my problem as simple as I can. I'm trying to make an app, which works with Yandex API. On their help page, I've read that you should launch browser from app where user logins and then return to application by registering URI Callback. What I have now:

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

This launches browser and redirects to authorization page. After entering login-password I return automatically to my app. Here's 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 = code ......等等。但我没有看到这个页面,因为重定向立刻回到应用程序。

And when I entered login-password I'm redirected to page like "yaruapp://token#access_token=YYYYYYYYYYYYYYY&token_type=code..." and so on. But I don't see this page because redirected instantly back to app.

现在的问题是:我如何能得到并提取这一部分:YYYYYYYYYYYYYY

我是这么noobish非常抱歉,希望你能帮助我。在此先感谢

I'm terribly sorry for being so noobish, hope you can help me. Thanks in advance

推荐答案

您可以得到onNewIntent的URI。只是覆盖在你的活动。你可以访问令牌与类似如下:

You can get the Uri in onNewIntent. Just override it in your Activity. You can get the access token with something like the following:

@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];
}

这篇关于我怎样才能从浏览器的意图授权令牌?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆