【问题标题】:Twitter oauth2 using javax.ws.rsTwitter oauth2 使用 javax.ws.rs
【发布时间】:2015-03-11 02:52:58
【问题描述】:

我有这个使用 javax.ws.rs 的 twitter 请求

    WebTarget target = new WebTargetBuilder(client, OAUTH_API_ENDPOINT).build();

    Builder request = target
            .request(MediaType.APPLICATION_JSON)
            .header("Authorization", "Basic " + encodedCredentials)
            .header("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
    Response postResponse = request
            .post(Entity.entity("grant_type=client_credentials", MediaType.TEXT_PLAIN));

    System.out.println(postResponse.readEntity(String.class));

encodedCredentials 是我的消费者密钥和以 base 64 编码的消费者密钥。

我正在尝试做的请求是:

POST /oauth2/token HTTP/1.1
Host: api.twitter.com
User-Agent: My Twitter App v1.0.23
Authorization: Basic eHZ6MWV2RlM0d0VFUFRHRUZQSEJvZzpMOHFxOVBaeVJn
                 NmllS0dFS2hab2xHQzB2SldMdzhpRUo4OERSZHlPZw==Content-Type: application/x-www-   form-urlencoded;charset=UTF-8
Content-Length: 29
Accept-Encoding: gzip

grant_type=client_credentials

我不断收到 403 Forbidden: {"errors":[{"code":170,"message":"Missing required parameter: grant_type","label":"forbidden_​​missing_parameter"}]}

好像post body没有设置好,有大佬知道怎么设置吗?

【问题讨论】:

    标签: java twitter oauth


    【解决方案1】:

    您可以尝试更改 POST 请求正文/实体的内容类型,如下所示:

     .post(Entity.entity("grant_type=client_credentials",  MediaType.APPLICATION_FORM_URLENCODED)
    

    【讨论】:

      【解决方案2】:

      我在 PHP 中使用过相同的,我认为你错过了像 oauth_signature 这样的必需参数

      【讨论】:

        【解决方案3】:

        因此,从 Twitter 获取具有消费者密钥和消费者秘密的不记名令牌的最终代码如下所示:

          private static final String OAUTH_API_ENDPOINT = "https://api.twitter.com/oauth2/token";
          private String consumerKey = "your consumer key";
          private String consumerSecret = "your consumer secret";
        
        // Constructs the request for requesting a bearer token and returns that
        // token as a string
        public String requestBearerToken() throws IOException, InterruptedException, ExecutionException {
        
            String encodedCredentials = encodeCredentials();
        
            Client client = ClientBuilder.newClient();
        
            WebTarget target = new WebTargetBuilder(client, OAUTH_API_ENDPOINT).build();
        
            Response postResponse = target
                    .request(MediaType.APPLICATION_JSON)
                    .header("Authorization", "Basic " + encodedCredentials + "Content-Type: application/x-www-form-urlencoded;charset=UTF-8")
                    .post(Entity.entity("grant_type=client_credentials", MediaType.APPLICATION_FORM_URLENCODED));
        
            return postResponse.toString();
        }
        
        // Encodes the consumer key and secret to create the basic authorization key
        public String encodeCredentials() {
            try {
                String encodedConsumerKey = URLEncoder.encode(consumerKey, "UTF-8");
                String encodedConsumerSecret = URLEncoder.encode(consumerSecret,
                        "UTF-8");
        
                String fullKey = encodedConsumerKey + ":" + encodedConsumerSecret;
                byte[] encodedBytes = Base64.encodeBase64(fullKey.getBytes());
                return new String(encodedBytes);
            } catch (UnsupportedEncodingException e) {
                return new String();
            }
        }
        

        【讨论】:

          猜你喜欢
          • 2020-11-24
          • 1970-01-01
          • 2011-09-18
          • 2016-01-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-08-04
          • 2011-08-27
          相关资源
          最近更新 更多