1
votes

I'm working on an android project that requires me to edit a user's GitHub info, like starring/unstarring repositories and following/unfollowing other users. These should be accomplished with PUT/DELETE requests. I generated a personal access token from my GitHub account, but I haven't been able to figure out how to get my requests to work. I know my token is fine, since I'm able to make the requests through POSTMAN and see the changes fine. The link I'm passing in is https://api.github.com/user/following/(user-im-trying-to-follow), as per the API documentation

    HttpsURLConnection connection = null;
    BufferedReader reader = null;
    try {


        URL url = new URL(link);

        connection = (HttpsURLConnection) url.openConnection();
        connection.setRequestMethod("PUT");
        connection.setDoOutput(true);
        String basicAuth = "Basic " +(Base64.encodeToString(ACCESS_TOKEN.getBytes(), Base64.NO_WRAP));
        connection.setRequestProperty("Authorization", basicAuth );

        connection.connect();


    } catch (ProtocolException e) {
        e.printStackTrace();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }finally {
        if(connection!=null) {
            connection.disconnect();
        }
    }

I tried replacing "Basic " with "token " since that's the value needed for the POSTMAN header, but it hasn't worked. What am I doing wrong?

1

1 Answers

0
votes

First, double-check the scope associated with your token:
It should include "user" (or at least user:follow)

Second, check out this example "How to Access Github Content with Basic OAuth Authentication in Java? HTTPClient Or URLConnection Method"

You see the URL used is:

String newUrl = "https://" + token + ":x-oauth-basic@" + url;

That could be an alternative approach to test.