当前位置: 首页 > 知识库问答 >
问题:

Twitter oauth无效oauth_verifier参数

拓拔耀
2023-03-14

我正在用Java实现oauth,顺序如下:

1) 发送帖子https://api。啁啾com/oauth/request_token(带回调)Twitter响应包含oauth_token、oauth_token_secret和oauth_callback_confirm=true

2) 正在重定向到https://api。啁啾com/oauth/authenticate?oauth_token={oauth_token来自twitter之前的响应}

3) Twitter登录表单出现时,我点击“登录”按钮。

4) Twitter重定向到{callback_url}?oauth_令牌={此令牌等于oauth/request_令牌响应中的令牌}

5) 发布https://api。啁啾带有oauth头的com/oauth/access_令牌包括oauth_令牌,消息体包含oauth_验证器={返回的验证器}

6) Twitter response=处理OAuth请求时出错:OAuth_验证器参数无效

oauth_验证器有什么问题?

计算签名方法:

private static String computeSignature(String baseString, String keyString) throws GeneralSecurityException, UnsupportedEncodingException {
        SecretKey secretKey = null;

        byte[] keyBytes = keyString.getBytes();
        secretKey = new SecretKeySpec(keyBytes, "HmacSHA1");

        Mac mac = Mac.getInstance("HmacSHA1");
        mac.init(secretKey);

        byte[] text = baseString.getBytes();

        return new String(Base64.encodeBase64(mac.doFinal(text))).trim();
    }

第一次请求的代码:

String oauth_signature_method = "HMAC-SHA1";

    // generate any fairly random alphanumeric string as the "nonce".
    String uuid_string = UUID.randomUUID().toString();
    uuid_string = uuid_string.replaceAll("-", "");
    String oauth_nonce = uuid_string;

    // get the timestamp
    Calendar tempcal = Calendar.getInstance();
    long ts = tempcal.getTimeInMillis();
    String oauth_timestamp = (new Long(ts / 1000)).toString(); 
    String parameter_string = "oauth_callback=" + OauthConstants.TWITTER_OAUTH_CALLBACK 
            + "&oauth_consumer_key=" + OauthConstants.TWITTER_OAUTH_CONSUMER_KEY
            + "&oauth_nonce=" + oauth_nonce + "&oauth_signature_method="
            + oauth_signature_method + "&oauth_timestamp=" + oauth_timestamp + "&oauth_version=1.0";
    String signature_base_string = get_or_post + "&" + encode(twitter_endpoint) + "&" + encode(parameter_string);
    String oauth_signature = "";

    try {
        oauth_signature = computeSignature(signature_base_string, OauthConstants.TWITTER_OAUTH_CONSUMER_SECRET + "&");  
    } catch (GeneralSecurityException | UnsupportedEncodingException e) {
       ...}

String twitter_endpoint = "https://api.twitter.com/oauth/request_token";
String authorization_header_string = "OAuth oauth_callback=\"" + OauthConstants.TWITTER_OAUTH_CALLBACK
                + "\",oauth_consumer_key=\"" + OauthConstants.TWITTER_OAUTH_CONSUMER_KEY
                + "\",oauth_signature_method=\"HMAC-SHA1\",oauth_timestamp=\"" + oauth_timestamp
                + "\",oauth_nonce=\"" + oauth_nonce + "\",oauth_version=\"1.0\",oauth_signature=\""
                + encode(oauth_signature) + "\"";

// Apache httpcore 4.4.1
HttpProcessor httpproc = HttpProcessorBuilder.create()
                .add(new RequestContent())
                .add(new RequestTargetHost())
                .add(new RequestConnControl())
                .add(new RequestUserAgent("ApacheHttp/1.1"))
                .add(new RequestExpectContinue(true)).build();

        HttpRequestExecutor httpexecutor = new HttpRequestExecutor();
        HttpCoreContext context = HttpCoreContext.create();
        HttpHost host = new HttpHost(twitter_endpoint_host, 443); 
        DefaultBHttpClientConnection conn = new DefaultBHttpClientConnection(8 * 1024);

        context.setAttribute(HttpCoreContext.HTTP_CONNECTION, conn);
        context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, host);

try {
                // initialize the HTTPS connection
                SSLContext sslcontext = SSLContext.getInstance("TLS");
                sslcontext.init(null, null, null);
                SSLSocketFactory ssf = sslcontext.getSocketFactory();
                Socket socket = ssf.createSocket();
                socket.connect(new InetSocketAddress(host.getHostName(), host.getPort()), 0);
                conn.bind(socket);

                BasicHttpEntityEnclosingRequest request2 = new BasicHttpEntityEnclosingRequest("POST", twitter_endpoint_path, HttpVersion.HTTP_1_1);
                request2.setEntity(new StringEntity("", "UTF-8"));
                request2.addHeader("Authorization", authorization_header_string); 
                httpexecutor.preProcess(request2, httpproc, context);
                HttpResponse response2 = httpexecutor.execute(request2, conn, context);
                httpexecutor.postProcess(response2, httpproc, context);
} catch(Exception e) {} ... 

第二个请求的代码(重定向到https oauth/authenticate)

public JSONObject getTwitterAuthorizationCodeFromRequestToken(String oauth_token) {
...
        String twitter_endpoint = "https://api.twitter.com/oauth/authenticate";

        ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect(twitter_endpoint  + "?oauth_token=" + encode(oauth_token));
        } catch (IOException ex) {...}
...
    }

第三次请求代码(POST oauth/access_令牌)

    public JSONObject getTwitterAccessTokenFromAuthorizationCode(String verifier_or_pin, String oauth_token) {
    ...
 String oauth_signature_method = "HMAC-SHA1";

    // generate any fairly random alphanumeric string as the "nonce". Nonce = Number used ONCE.
    String uuid_string = UUID.randomUUID().toString();
    uuid_string = uuid_string.replaceAll("-", "");
    String oauth_nonce = uuid_string; 

    Calendar tempcal = Calendar.getInstance();
    long ts = tempcal.getTimeInMillis();
    String oauth_timestamp = (new Long(ts / 1000)).toString(); 

    // the parameter string must be in alphabetical order
    String parameter_string = "oauth_consumer_key=" + OauthConstants.TWITTER_OAUTH_CONSUMER_KEY 
                            + "&oauth_nonce=" + oauth_nonce + "&oauth_signature_method=" + oauth_signature_method
                            + "&oauth_timestamp=" + oauth_timestamp + "&oauth_token=" + encode(oauth_token) + "&oauth_version=1.0";

    String signature_base_string = get_or_post + "&" + encode(twitter_endpoint) + "&" + encode(parameter_string);

    String oauth_signature = "";
    try {
        oauth_signature = computeSignature(signature_base_string, OauthConstants.TWITTER_OAUTH_CONSUMER_SECRET + "&");
    } catch (GeneralSecurityException | UnsupportedEncodingException e) {
        ...
    }

    String authorization_header_string = "OAuth oauth_consumer_key=\"" + OauthConstants.TWITTER_OAUTH_CONSUMER_KEY  
                                               + "\",oauth_signature_method=\"HMAC-SHA1\",oauth_timestamp=\"" + oauth_timestamp
                                               + "\",oauth_nonce=\"" + oauth_nonce + "\",oauth_version=\"1.0\",oauth_signature=\"" 
                                               + encode(oauth_signature) + "\",oauth_token=\"" + encode(oauth_token) + "\"";

    HttpProcessor httpproc = HttpProcessorBuilder.create()
                    .add(new RequestContent())
                    .add(new RequestTargetHost())
                    .add(new RequestConnControl())
                    .add(new RequestUserAgent("ApacheHttp/1.1"))
                    .add(new RequestExpectContinue(true)).build();

            HttpRequestExecutor httpexecutor = new HttpRequestExecutor();
            HttpCoreContext context = HttpCoreContext.create();
            HttpHost host = new HttpHost(twitter_endpoint_host, 443); 
            DefaultBHttpClientConnection conn = new DefaultBHttpClientConnection(8 * 1024);

            context.setAttribute(HttpCoreContext.HTTP_CONNECTION, conn);
            context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, host);

    try {
                    SSLContext sslcontext = SSLContext.getInstance("TLS");
                    sslcontext.init(null, null, null);
                    SSLSocketFactory ssf = sslcontext.getSocketFactory();
                    Socket socket = ssf.createSocket();
                    socket.connect(new InetSocketAddress(host.getHostName(), host.getPort()), 0);
                    conn.bind(socket);

                    BasicHttpEntityEnclosingRequest request2 = new BasicHttpEntityEnclosingRequest("POST", twitter_endpoint_path);
                    // Including oauth_verifier value to request body
                    request2.setEntity(new StringEntity("oauth_verifier=" + encode(verifier_or_pin), "UTF-8"));
                    request2.addHeader("Authorization", authorization_header_string);
                    httpexecutor.preProcess(request2, httpproc, context);
                    HttpResponse response2 = httpexecutor.execute(request2, conn, context);
... 
    }

共有1个答案

樊奇思
2023-03-14

我在JavaScript代码库中也遇到过同样的情况。经过一天的努力,我找到了解决错误的方法。这只是添加了一个值为“application/x-www-form-urlencoded”的“Content-Type”标题。

我的代码曾经正常工作,但它在过去几个月停止工作。我的猜测是Twitter最近更改了OAuth处理的实现,这迫使我们显式添加内容类型。

 类似资料:
  • 我在跟踪https://dev.twitter.com/web/sign-in/implementing使用twitter在我的应用程序中实现OAuth注册。 这是从twitter向用户显示授权此应用程序对话框的服务: 这可以工作并将浏览器重定向到请求授权的Twitter页面。当我在重定向对话框中单击“登录”时,会将我重定向到URL,类似于: https://localhost:9090/app/

  • 问题内容: 有人知道为什么chrome参数不起作用吗? 我也尝试过在切换台之前不加“-”并仅给出一个参数… Chrome启动,但没有激活的标志。 我使用最新的chromedriver。 问题答案: 我最近发现的类不与当前的硒和铬出于某种原因(如硒2.33.0,铬30和2013年7月)正常工作。 我相信,我链接的答案也是您解决问题的方法。只需使用,这些对我来说一直很好:

  • 我想让它在每天每小时的第5,15,25,35,45和55分钟运行。这似乎与这里的AWS调度事件文档一致http://docs.AWS.amazon.com/amazoncloudwatch/latest/Events/scheduledevents.html。 上面的文档允许用0到59之间的逗号分隔值表示分钟,并使用*通配符反映小时、月日(或周日)、月和年。 我尝试在Lambda控制台(创建函数并

  • 我正在上载excel工作表,并希望将其数据移动到数据库中的另一个表中。最近两天我收到了一个错误(),但是,如果我设计了一个非常简单的应用程序,它就可以正常工作。请帮帮我

  • 当我试图从我的api中点击以从keydove对用户进行身份验证时,它给了我一个错误无效参数:keydove页面上的redirect_uri。除了师父,我创造了自己的王国。KeyClope正在http上运行。请帮帮我。

  • 当用户单击“登录”时,会重定向到KeyClope登录页面 [Server:node-00]13:40:00709警告[org.keydape.adapters.OAuthRequestAuthenticator](默认任务-30)状态参数无效 我的申请是: 应用程序URL为https://localhost:8443/app 当我使用超文本传输协议时,它可以工作。但是当我使用https时,错误就来