Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7664607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:20:57+00:00 2026-05-31T14:20:57+00:00

I can’t figure this one out. I’m trying to dynamically roll keys. I can

  • 0

I can’t figure this one out. I’m trying to dynamically roll keys. I can create the POST request fine, but receive a 400 error and a stacktrace with an IOException when I call post. Below is a self-contained example. I’m using JSCH to generate keys. API doc: http://developer.github.com/v3/users/keys/

The API call: POST /user/keys

public static class LiberalHostnameVerifier implements HostnameVerifier {
    public boolean verify(String hostname, SSLSession session) {
        return true;
    }
}

public static String post(String requestUrl, Map<String, String> params,
    String username, String password) throws Exception {
    String data = "";
    int paramCount = 1;
    for (Entry<String, String> param : params.entrySet()) {
        if (paramCount == 1) {
            data = URLEncoder.encode(param.getKey(), "UTF-8") + "="
                + URLEncoder.encode(param.getValue(), "UTF-8");
        } else {
            data += "&" + URLEncoder.encode(param.getKey(), "UTF-8") + "="
                + URLEncoder.encode(param.getValue(), "UTF-8");
        }
        paramCount++;
    }
    URL url = new URL(requestUrl);
    HttpsURLConnection conn = (HttpsURLConnection) (url).openConnection();
    conn.setDoOutput(true);
    conn.setRequestMethod("POST");
    conn.setHostnameVerifier(new LiberalHostnameVerifier());
    BASE64Encoder enc = new BASE64Encoder();
    String userAuth = username + ":" + password;
    String encodedAuthorization = enc.encode(userAuth.getBytes());
    conn.setRequestProperty("Authorization", "Basic " + encodedAuthorization);
    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
    wr.write(data);
    wr.flush();
    BufferedReader rd = new BufferedReader(new InputStreamReader(
        conn.getInputStream()));
    String line;
    String response = "";
    while ((line = rd.readLine()) != null) {
        response += line;
    }
    wr.close();
    rd.close();
    return response;
}

public static KeyPair generateKey(String filename) throws Exception {
    JSch jsch = new JSch();
    try {
        KeyPair kpair = KeyPair.genKeyPair(jsch, KeyPair.RSA);
        kpair.setPassphrase("");
        kpair.writePrivateKey(filename + ".pem");
        kpair.writePublicKey(filename + ".pub", "Auto-generated.");
        System.out.println("Finger print: " + kpair.getFingerPrint());
        // kpair.dispose();
        return kpair;
    } catch (Exception e) {
        System.out.println(e);
    }
    return null;
}

public static String getFileContents(File file) throws Exception {
    byte[] buffer = new byte[(int) file.length()];
    FileInputStream f = new FileInputStream(file);
    f.read(buffer);
    return new String(buffer);
}

public static String createKey(String title) throws Exception {
    generateKey(title);
    final String key = getFileContents(new File(
            "/Users/franklovecchio/Desktop/development/" + title
                + ".pub"));
    System.out.println("key: " + key);
    Map<String, String> params = new HashMap<String, String>() {

        {
            put("title", title);
            put("key", key);
        }
    };
    return post("https://api.github.com/user/keys", params, "username",
        "password");
}

// call createKey(“key);

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T14:20:59+00:00Added an answer on May 31, 2026 at 2:20 pm

    Thanks to @nico_ekito and @J-16 SDiZ for helping in the right direction. If you look closely at the documentation, the request doesn’t use standard POST parameters, but rather takes JSON as Raw Input, and the ssh-rsa key can NOT be encoded. Next up, I can’t get GSON to not encode a string, even using disableHtmlEscaping. So, I had to fake it:

    String json = "{\"title\":\"" + title + "\",\"key\":\"" + key.trim() + "\"}";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can't figure out how to do this in a pretty way : I have
Can any one help me in sorting this out in sed/awk/perl Input file Start
Can anyone help me trying to find out why this doesn't work. The brushes
Can't seem to figure out what's wrong with the simple getJSON call below. It's
Can you create a complete website with only one page? So using XHTML, CSS,
Can any one tell, how to get the result of LINQ query contains group
Can anyone explain to me why this program: for(float i = -1; i <
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Basically, what I'm trying to create is a page of div tags, each has
Can I run this in a Windows command prompt like I can run it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.