HttpURLConnection我在哪里放置API密钥?

时间:2018-01-03 02:01:02

标签: android json httpurlconnection api-key

我正在开发一个Android应用程序,我正在尝试从Web API检索JSON对象。我能够阅读公共端点并显示结果。但是,当我尝试读取私有端点时,我在控制台中收到错误“Permission Denied”。我已阅读API文档,并声明将api密钥放在标头中。它给出了一个例子:

# Example with curl

curl -X GET https://wger.de/api/v2/workout/ \

-H 'Authorization: Token ae45ad5c68667c4f3a5bfcd983b629875732ecbb'

这是我的帖子请求类:

public void sendPostRequest (String where) {
        URL loc = null;
        HttpURLConnection conn = null;
        InputStreamReader is;
        BufferedReader in;

        try {
            loc = new URL(where);
        }
        catch (MalformedURLException ex) {
            return;
        }

        try {
            conn = (HttpURLConnection)loc.openConnection();
            conn.addRequestProperty("Authorization","my-api-key");
            is = new InputStreamReader (conn.getInputStream(), "UTF-8");
            in = new BufferedReader (is);

            readResponse (in);
        }
        catch (IOException ex) {

        }
        finally {
            conn.disconnect();
        }

    }

我尝试添加conn.addRequestProperty(“授权”,“my-api-key”);但是我的应用程序在尝试重新接收JSON对象时失败了。

0 个答案:

没有答案