首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用oauth2 javax.ws.rs的Twitter

使用oauth2 javax.ws.rs的Twitter
EN

Stack Overflow用户
提问于 2015-01-13 07:52:12
回答 3查看 1.3K关注 0票数 5

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

代码语言:javascript
复制
    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是我的使用者秘密和用户密钥,编码在基数64中。

我想要做的是:

代码语言:javascript
复制
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禁止:{“错误”:{“代码”:170,“消息”:“缺少必需的参数: grant_type",”标签“:”forbidden_missing_parameter“}}

似乎帖子的主体设置不正确,有人知道怎么设置吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-01-13 09:50:45

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

代码语言:javascript
复制
 .post(Entity.entity("grant_type=client_credentials",  MediaType.APPLICATION_FORM_URLENCODED)
票数 2
EN

Stack Overflow用户

发布于 2015-01-13 09:31:35

我在PHP中使用过相同的参数,我认为您忽略了所需的参数,比如oauth_signature。

票数 0
EN

Stack Overflow用户

发布于 2015-01-13 10:01:02

因此,从Twitter获得具有使用者密钥和消费者秘密的承载令牌的最终代码如下所示:

代码语言:javascript
复制
  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();
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27917249

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档