3

请帮帮我。我为 github.com api v3 制作了 android 客户端,但我遇到了授权问题。(登录 = mytest12345 ,通过 = 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"

# 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())

这是我的代码:

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);

BufferedReader reader = new BufferedReader(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错误

4

3 回答 3

3

我知道你想要它在 Java 中,但你可能对我刚刚为另一个问题提供的答案感兴趣,以获取更多 Python 示例。

为了进行身份验证,我必须明确说明身份验证的类型(以下是 Python 代码):

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

但是您似乎没有添加“基本”前缀。尝试添加它,看看你是否成功。

于 2012-07-23T22:44:16.150 回答
0
def encoded = Base64.getEncoder().encodeToString((username + ":" + password).getBytes());
request.setHeader("Authorization", "Basic " + encoded);
于 2016-06-30T08:26:16.753 回答
0

最后,在浪费了一整天之后,我得到了它。这是我的工作代码。只需传递 uName 和密码,它将返回 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;
}

参考链接:Link1 & Link for basci Auth & Link for web flow:Android

于 2016-12-16T16:52:25.407 回答