无法通过访问令牌获取用户信息

时间:2015-12-31 01:46:37

标签: java android oauth-2.0 client-server bitbucket-api

我试图通过access_token获取有关用户的信息...我已经尝试了很多变体来做到这一点。虽然某些变体可以在API控制台中使用,但它们不适用于应用程序。

    public class AuthActivity extends AccountAuthenticatorCompatActivity {
    private static final String TAG = AuthActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_auth);
        Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(vcs.getAccessRequest()));
        startActivity(i);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        Uri data = intent.getData();

        if(null != data) {
            final String accessToken = data.getQueryParameter("code");
            AuthTask authTask = new AuthTask(accessToken);
            authTask.execute();
        }
    }


    private class AuthTask extends AsyncTask<Void, Void, String> {
        private final String mAccessToken;
        private final VCS vcs = new Bitbucket();

        public AuthTask(final String accessToken) {
            mAccessToken = accessToken;
        }

        @Override
        protected String doInBackground(Void... params) {

            try {
                URL url = new URL("https://api.bitbucket.org/2.0/user");
                HttpsURLConnection connection = (HttpsURLConnection)url.openConnection();
                connection.addRequestProperty("Authorization", "Bearer " + mAccessToken);
                connection.setRequestMethod("GET");
                //connection.setRequestProperty("access_token", mAccessToken);
                int responseCode = connection.getResponseCode();

                BufferedReader in = new BufferedReader(
                        new InputStreamReader(connection.getInputStream()));
                String inputLine;
                StringBuilder response = new StringBuilder();

                while ((inputLine = in.readLine()) != null) {
                    response.append(inputLine);
                }

                in.close();
                return response.toString();
            } catch (IOException e) {
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(String str) {
        }
    }

}

所有请求都适用于访问令牌不需要的地方。但我不能使用令牌访问。我真的尝试了很多方法。请发布可行的代码。我做错了什么?我不明白。

0 个答案:

没有答案
相关问题