0

我正在尝试通过 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) {
        }
    }

}

所有请求都在不需要访问令牌的情况下工作。但我不能使用令牌访问。我真的尝试了很多方法。请张贴可以工作的代码。我做错了什么?我不明白。

4

0 回答 0