如何获取YouTube身份验证令牌?

时间:2019-11-28 09:16:25

标签: android oauth youtube-data-api

我正在尝试获取YouTube身份验证令牌,以便在用户登录后加载youtube页面。我正在尝试遵循示例here。但是回调AccountManagerCallback永远不会被回调。 这是我的代码版本。

final String[] SCOPES = { YouTubeScopes.YOUTUBE_READONLY };
    mCredential = GoogleAccountCredential.usingOAuth2(
            getApplicationContext(), Arrays.asList(SCOPES))
            .setBackOff(new ExponentialBackOff());
...
startActivityForResult(mCredential.newChooseAccountIntent(),
                       REQUEST_ACCOUNT_PICKER);  // list all google accounts

...
protected void onActivityResult(final int requestCode, final int resultCode,
                                final Intent data) {
    if (requestCode == REQUEST_ACCOUNT_PICKER && resultCode == RESULT_OK) {
        String accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
        AuthManager am = new AuthManager();
        am.getAccount(this, accountName);
    }
}
....

// in a different class
public void getAccount(Activity activity, final String accountName) {
    mActivity = activity;
    authenticate(accountName);
}

private void authenticate(final String accountName) {
    Bundle options = new Bundle();
    Log.d(TAG, "Account name is: " + accountName);
    AccountManager accountManager = AccountManager.get(mActivity.getApplicationContext());
    Account[] accounts = accountManager.getAccountsByType("com.google");
    for (Account account : accounts) {
        Log.d(TAG, "account: " + account.name + " : " + account.type);
        if (account.name.equals(accountName)) {
            Log.d(TAG, "Match");
            accountManager.getAuthToken(
                    account,                     // Account retrieved using getAccountsByType()
                    "oauth2:https://www.youtube.com/",            // Auth scope
                    options,
                    true,                          // Your activity
                    new OnTokenAcquired(),          // Callback called when a token is successfully acquired
                    new Handler(new OnError()));    // Callback called if an error occurs
        }
    }
}

private class OnTokenAcquired implements AccountManagerCallback<Bundle> {
    @Override
    public void run(AccountManagerFuture<Bundle> result) {
        try {
            // 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 constant AccountManager.KEY_AUTHTOKEN.
            String token = bundle.getString(AccountManager.KEY_AUTHTOKEN);
            Log.d(TAG, "The obtained token is: " + token);
        } catch (AuthenticatorException ae) {
            ae.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } catch (OperationCanceledException oce) {
            oce.printStackTrace();
        }
    }
}   

public class OnError implements Handler.Callback {

    @Override
    public boolean handleMessage(@NonNull Message msg) {
        Log.d(TAG, "" +  msg);
        return false;
    }
}

我正确获取了帐户列表,但是未调用OnTokenAcquired回调。这段代码有什么问题?谢谢。

0 个答案:

没有答案