Autorización para github.com en Android

Ayudame por favor. Hago cliente para Android para github.com api v3 y tengo un problema con la autorización. (inicio de session = mytest12345, pase = 12345testing)

http://developer.github.com/v3/

Ejemplo de autorización de Python http://agrimmsreality.blogspot.com/2012/05/sampling-github-api-v3-in-python.html

serverurl="https://api.github.com" # Add your username and password here, or prompt for them auth=BasicAuth(user, password) # Use your basic auth to request a token # This is just an example from http://developer.github.com/v3/ authreqdata = { "scopes": [ "public_repo" ], " note": "admin script" } resource = Resource('https://api.github.com/authorizations', pool=pool, filters=[auth]) response = resource.post(headers={ "Content-Type": "application/json" }, payload=json.dumps(authreqdata)) token = json.loads(response.body_string())['token'] """ Once you have a token, you can pass that in the Authorization header You can store this in a cache and throw away the user/password This is just an example query. See http://developer.github.com/v3/ for more about the url structure """ resource = Resource('https://api.github.com/user/repos', pool=pool) headers = {'Content-Type' : 'application/json' } headers['Authorization'] = 'token %s' % token response = resource.get(headers = headers) repos = json.loads(response.body_string()) 

Este es mi código:

 public String Authorization(){ String result = new String(""); try{ HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost("https://api.github.com"); post.setHeader("https://api.github.com/authorizations", Base64.encodeToString("mytest12345:12345test".getBytes(), Base64.DEFAULT)); HttpParams pers; JSONObject json = new JSONObject("{\"scopes\": [\"public_repo\"],\"note\": \"admin script\"}"); StringEntity se = new StringEntity( json.toString()); se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); post.setEntity(se); StringBuilder builder = new StringBuilder(); HttpResponse response = client.execute(post); BuffenetworkingReader reader = new BuffenetworkingReader(new InputStreamReader(response.getEntity().getContent(),"windows-1251")); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + System.getProperty("line.separator")); } result = sb.toString(); } catch (org.apache.http.client.ClientProtocolException e) { result = "ClientProtocolException: " + e.getMessage(); } catch (IOException e) { result = "IOException: " + e.getMessage(); } catch (Exception e) { result = "Exception: " + e.getMessage(); } return result; } 

500 error en final

Sé que lo quieres en Java, pero podría interesarte una respuesta que acabo de proporcionar a otra pregunta para get más ejemplos de Python.

Para autenticar tuve que especificar específicamente el tipo de authentication (lo que sigue es el código de Python):

 base64string = base64.encodestring('%s:%s' % (username, passwd)).replace('\n', '') req.add_header("Authorization", "Basic %s" % base64string) 

Pero no parece que esté agregando el prefijo "Básico". Intenta agregar eso y ver si tienes éxito.

 def encoded = Base64.getEncoder().encodeToString((username + ":" + password).getBytes()); request.setHeader("Authorization", "Basic " + encoded); 

Finalmente, después de perder un día completo lo obtuve. Este es mi código de trabajo. Simplemente pase uName y password, devolverá la cadena JSON.

 public String Authorization(String username, String password) { String result = null; HttpClient client = new DefaultHttpClient(); HttpGet httpGet = new HttpGet("https://api.github.com/user"); String encode = Base64.encodeToString((username + ":" + password).getBytes(), Base64.DEFAULT).replace("\n", ""); httpGet.setHeader("Authorization", "Basic " + encode); try { HttpResponse httpResponse = client.execute(httpGet); InputStream inputStream = httpResponse.getEntity().getContent(); InputStreamReader inputStreamReader = new InputStreamReader(inputStream); BuffenetworkingReader buffenetworkingReader = new BuffenetworkingReader(inputStreamReader); StringBuilder stringBuilder = new StringBuilder(); String buffenetworkingStrChunk = null; while ((buffenetworkingStrChunk = buffenetworkingReader.readLine()) != null) { stringBuilder.append(buffenetworkingStrChunk); } result = stringBuilder.toString(); } catch (ClientProtocolException cpe) { System.out.println("ClientProtocolException :" + cpe); cpe.printStackTrace(); } catch (IOException ioe) { System.out.println("IOException :" + ioe); ioe.printStackTrace(); } return result; } 

Enlaces de reference: Link1 & Link para basci Auth & Link para flujo web: Android