授权github.com在Android [英] Authorization for github.com on Android

查看:200
本文介绍了授权github.com在Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助我。我做客户端Android版的github.com API v3和我有权限的麻烦。 (登录= mytest12345,通= 12345test)

Help me please. I make client for android for github.com api v3 and i have a trouble with authorization. (login = mytest12345 , pass = 12345test)

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

Python的授权样的http://agrimmsreality.blogspot.com/2012/05/sampling-github-api-v3-in-python.html

Python authorization sample 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())

这是我的code:

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错误在最后的

500 error in final

推荐答案

我知道你想让它在Java中,但你可能有兴趣的我只是提供给另一个问题的答案的进一步Python的例子。

I know you want it in Java, but you might be interested in an answer I just provided to another question for further Python examples.

为了验证我必须具体说明身份验证的类型(接下来是Python的code):

In order to authenticate I had to specifically state the type of authentication (what follows is Python code):

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

但它没有出现您要添加的基本preFIX。尝试添加的,如果你得到任何成功的看到。

But it doesn't appear that you are adding the "Basic " prefix. Try adding that in and seeing if you get any success.

这篇关于授权github.com在Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆