0

I am trying to get information about user by access_token... I have tried a lot of variants for doing this. Although some variants work in the API console but they don't in application.

    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) {
        }
    }

}

All requests are work where access token not need. But I cannot use access by token. I really try a lot of ways. Please, post code that will be work. What I do wrong? I don't understand.

Denis Sologub
  • 7,277
  • 11
  • 56
  • 123
  • 1
    Don't use the https://bitbucket.org/api domain. It does not run the API and does not support OAuth. Always use https://api.bitbucket.org – Erik van Zijst Dec 31 '15 at 04:45
  • Thanks! But it doesn't resolve my problem, unfortunately. – Denis Sologub Dec 31 '15 at 07:05
  • I'm glad that you here too :) – Denis Sologub Dec 31 '15 at 07:07
  • 2
    Here's an example request: https://bitbucket.org/snippets/evzijst/8K4xn If you really can't make this work I suggest you contact us at support@bitbucket.org or erik@atlassian.com and we can help you troubleshoot. Make sure your access token has not expired and include all the relevant HTTP logs and the output of your program (and contents of the response body that we sent you). Pro-tip: a crude way to verify your client is sending what you think it sending, change the final URL to something on localhost (http://localhost:12345) and run a fake webserver to intercept it like so: $ nc -l 12345 – Erik van Zijst Dec 31 '15 at 09:02

0 Answers0