请帮帮我。我为github.com api v3制作了android的客户端,我遇到了麻烦 授权 </跨度> 。 (login = mytest12345,pass = 12345test)
http://developer.github.com/v3/
Python 授权 </跨度> 示例http://agrimmsreality.blogspot.com/2012/05/sampling-github-api-v3-in-python.html
SERVERURL = “https://api.github.com”
#在这里添加您的用户名和密码,或提示他们auth = BasicAuth
最后,在浪费了一整天后,我得到了它。这是我的工作代码。只需通过uName&amp;密码,它将返回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); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); StringBuilder stringBuilder = new StringBuilder(); String bufferedStrChunk = null; while ((bufferedStrChunk = bufferedReader.readLine()) != null) { stringBuilder.append(bufferedStrChunk); } 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; }
参考链接: 链接1 &安培; basci Auth的链接 &安培; 网络流量链接:Android
def encoded = Base64.getEncoder().encodeToString((username + ":" + password).getBytes()); request.setHeader("Authorization", "Basic " + encoded);