oauth2.0

2017-02-28  本文已影响0人  BridgeXD
原理图 流程图

1 四个角色

2 OAuth2.0六个流程

    <manifest ... >
        <uses-permission android:name="android.permission.ACCOUNT_MANAGER" />
        <uses-permission android:name="android.permission.INTERNET" />
        ...
    </manifest>

调用方法AccountManager很棘手!由于帐户操作可能涉及网络通信,大多数的方法都是异步AccountManager。这意味着,而不是做你所有的认证工作的一个功能,你需要一系列的回调函数实现。例如:

AccountManager am = AccountManager.get(this);
Bundle options = new Bundle();

am.getAuthToken(
myAccount_,                     // Account retrieved using getAccountsByType()
"Manage your tasks",            // Auth scope
options,                        // Authenticator-specific options
this,                           // Your activity
new OnTokenAcquired(),          // Callback called when a token is successfully acquired
new Handler(new OnError()));    // Callback called if an error occurs

Your first request for an auth token might fail for several reasons:

In this example, OnTokenAcquired is a class that implements AccountManagerCallback. AccountManager calls run() on OnTokenAcquired with an AccountManagerFuture that contains a Bundle. If the call succeeded, the token is inside the Bundle.

    private class OnTokenAcquired implements AccountManagerCallback<Bundle> {
    @Override
    public void run(AccountManagerFuture<Bundle> result) {
    // Get the result of the operation from the AccountManagerFuture.
    Bundle bundle = result.getResult();

    // The token is a named value in the bundle. The name of the value
    // is stored in the constant AccountManager.KEY_AUTHTOKEN.
    token = bundle.getString(AccountManager.KEY_AUTHTOKEN);
        ...
     }
    }
上一篇 下一篇

猜你喜欢

热点阅读