android - Android 上对 github.com 的授权

标签 android post github authorization

请帮帮我。我为 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错误

最佳答案

我知道您需要 Java 版,但您可能对 an answer I just provided to another question 感兴趣有关更多 Python 示例。

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

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

但是您似乎没有添加“Basic”前缀。尝试将其添加进去,看看是否成功。

关于android - Android 上对 github.com 的授权,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11590248/

相关文章:

Android CursorLoader 和 LoaderManager 错误

javascript - Vert.x 的 POST 请求和 JavaScript 有什么区别?

git - 可以将 repo 克隆到 Docker 容器吗?

facebook - GitHub 社交媒体图像在更新时未刷新

android - Activity.setContentView,View.setContentView?

android - 如何使 UC 浏览器像底部栏中的设置弹出菜单?

java - 当我的在线状态发生变化时,Android 广播接收器崩溃

wordpress - 为 wordpress 安装 curl POST 请求

python - 如何使用 django/python 从网络服务获取返回值

github - 如何将 Launchpad 项目迁移到 GitHub,包括 Bug/Issues?