我怎样才能获得授权码?

时间:2015-08-17 13:44:44

标签: android google-plus accountmanager

对于我的服务器部件应用程序需要auth令牌(用于身份验证)和“授权代码”以获取一些信息。 我在我的Android应用程序上获得auth令牌并将其发送到服务器

private void requestToken() {
        Account userAccount = null;
        String user = Settings.getUser(getActivity());
        for (Account account : accountManager.getAccountsByType("com.google")) {
            if (account.name.equals(user)) {
                userAccount = account;
                break;
            }
        }
        accountManager.getAuthToken(userAccount, "oauth2:https://www.googleapis.com/auth/plus.login",
                null, getActivity(),
                new OnTokenAcquired(), null);
    }

private class OnTokenAcquired implements AccountManagerCallback<Bundle> {
        @Override
        public void run(AccountManagerFuture<Bundle> result) {
            try {
                Bundle bundle = result.getResult();
                Intent launch = (Intent) bundle.get(AccountManager.KEY_INTENT);
                dumpIntent(launch);
                if (launch != null) {
                    startActivityForResult(launch, AUTHORIZATION_CODE);
                } else {
                    String token = bundle
                            .getString(AccountManager.KEY_AUTHTOKEN);
                    Settings.setToken(getActivity(), token);
                    doCoolAuthenticatedStuff();
                }
            } catch (Exception e) {
                Toast.makeText(getActivity(), R.string.error_check_connection, Toast.LENGTH_SHORT).show();
                //throw new RuntimeException(e);
            }
        }
    }

如何使用此代码或其他代码解决我的任务以获取授权代码?

0 个答案:

没有答案