【发布时间】:2018-04-09 15:15:08
【问题描述】:
我正在开发一个 android 项目,该项目需要我编辑用户的 GitHub 信息,例如为存储库加星标/取消星标以及关注/取消关注其他用户。这些应该通过 PUT/DELETE 请求来完成。我从我的 GitHub 帐户生成了一个个人访问令牌,但我无法弄清楚如何让我的请求正常工作。我知道我的令牌很好,因为我可以通过 POSTMAN 发出请求并看到更改正常。根据 API 文档,我传入的链接是 https://api.github.com/user/following/(user-im-trying-to-follow)
HttpsURLConnection connection = null;
BufferedReader reader = null;
try {
URL url = new URL(link);
connection = (HttpsURLConnection) url.openConnection();
connection.setRequestMethod("PUT");
connection.setDoOutput(true);
String basicAuth = "Basic " +(Base64.encodeToString(ACCESS_TOKEN.getBytes(), Base64.NO_WRAP));
connection.setRequestProperty("Authorization", basicAuth );
connection.connect();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(connection!=null) {
connection.disconnect();
}
}
我尝试将“Basic”替换为“token”,因为这是 POSTMAN 标头所需的值,但没有奏效。我做错了什么?
【问题讨论】:
标签: android github-api