如何在Android上授权,在Android上授权github.com

作者在使用Android应用访问GitHub API v3时,遇到登录授权问题。尝试通过Basic Auth方式并设置Scopes但遭遇500错误。代码展示了认证请求和错误处理过程。寻求解决如何正确获取和使用OAuth token以避免权限问题。
摘要由CSDN通过智能技术生成

请帮帮我.我为github.com api v3制作了android的客户端,我在授权方面遇到了麻烦. (login = mytest12345,pass = 12345test)

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错误

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值