【问题标题】:Sending PUT and DELETE requests to GItHub API via HTTPSUrlConnection通过 HTTPSUrlConnection 向 GItHub API 发送 PUT 和 DELETE 请求
【发布时间】: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


    【解决方案1】:

    首先,double-check the scope associated with your token:
    它应该包括“user”(或至少 user:follow)

    其次,看看这个例子“How to Access Github Content with Basic OAuth Authentication in Java? HTTPClient Or URLConnection Method”

    您看到使用的 URL 是:

    String newUrl = "https://" + token + ":x-oauth-basic@" + url;
    

    这可能是另一种测试方法。

    【讨论】:

      猜你喜欢
      • 2016-01-06
      • 1970-01-01
      • 2015-07-10
      • 1970-01-01
      • 1970-01-01
      • 2011-01-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多