简体   繁体   English

如何通过HTTP get请求发送授权密钥?

[英]How do I send an authorization key with my HTTP get request?

I'm trying to send a HTTP request to a REST API which requires and authorization key. 我正在尝试将HTTP请求发送到需要API和授权密钥的REST API。 My code works for REST API's that don't need authorization, but with this one, I only get error 403. I need help 我的代码适用于不需要授权的REST API,但是有了这一代码,我只会收到错误403。我需要帮助

The .setRequestProperty("Authorization", key) doesn't work. .setRequestProperty(“ Authorization”,key)不起作用。 I've tried sending my key with "Bearer " +, but still nothing. 我尝试用“ Bearer” +发送密钥,但是还是没有。

Here's the api: https://developer.clashroyale.com/#/getting-started 这是api: https//developer.clashroyale.com/#/getting-started

        import java.io.BufferedReader;
        import java.io.IOException;
        import java.io.InputStreamReader;
        import java.net.HttpURLConnection;
        import java.net.MalformedURLException;
        import java.net.URL;

        public class Json {

            public static void main(String[] args) {

                try {

                    String key;
                    URL url = new URL("https://api.clashroyale.com/v1/players/%23PPCY9Y2J/upcomingchests");
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    conn.setRequestMethod("GET");
                    conn.setRequestProperty("Accept", "application/json");
                    conn.setRequestProperty("Content-Type", "application/json");
                    conn.setRequestProperty("Authorization", key); 
                    if (conn.getResponseCode() != 200) {
                        throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
                    }

                    BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));

                    String output;
                    System.out.println("Output from Server .... \n");
                    while ((output = br.readLine()) != null) {
                        System.out.println(output);
                    }

                    conn.disconnect();

                } catch (MalformedURLException e) {

                    e.printStackTrace();

                } catch (IOException e) {

                    e.printStackTrace();

                }

            }

        }

When you go to the documentation in https://developer.clashroyale.com/#/documentation and you select to "Try out" a request it shows an example request using curl. 当您转到https://developer.clashroyale.com/#/documentation中的文档,并且选择“尝试”一个请求时,它将显示一个使用curl的示例请求。 For example: 例如:

curl -X GET --header 'Accept: application/json' --header " authorization: Bearer (token) " ' https://api.clashroyale.com/v1/locations?limit=5 ' curl -X GET --header'接受:application / json'--header“ 授权:Bearer(令牌) ”' https://api.clashroyale.com/v1/locations?limit=5 '

There, you can see that "Authorization" is all lowercase and that you need "Bearer " before your key. 在那里,您可以看到“授权”全部为小写字母,并且在密钥之前需要“承载者”。

Therefore, change this line: 因此,更改此行:

  conn.setRequestProperty("Authorization", key); 

For: 对于:

  conn.setRequestProperty("authorization", "Bearer " + key); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM