1

我在 singpost 有问题,应该在重定向 oauth_verifier 后返回,但会在切换到浏览器后返回相同地址的帖子,随后返回回调的授权是 oauth_verifier。我会以某种方式自动化,或者在我的程序中只是一个错误?附上您的代码,并感谢您的回复。

 class LoginThread extends Thread {
    String UserT;
    String PassT;
    CommonsHttpOAuthConsumer consumer;
    CommonsHttpOAuthProvider provider;
    LoginThread(String user,String pass){
        this.UserT = user;
        this.PassT = pass;
        consumer = new CommonsHttpOAuthConsumer("******0", "***y");
        provider = new CommonsHttpOAuthProvider("http://dev.senosec.czu.cz/api/auth/request-token","http://dev.senosec.czu.cz/api/auth/access-token","http://dev.senosec.czu.cz/api/user/login");
    }
    @Override
    public void run(){
        try {
            CookieManager manager = new CookieManager();
            manager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
            CookieHandler.setDefault(manager);


            String loginDialogAddress = provider.retrieveRequestToken(consumer,"http://dev.senosec.czu.cz/api/user/login");
            StringBuilder loginParametersBuilder = new StringBuilder();
            loginParametersBuilder.append("&email=").append(URLEncoder.encode(userT,"UTF-8"));
            loginParametersBuilder.append("&password=").append(URLEncoder.encode(passT,"UTF-8"));
            loginParametersBuilder.append("&login=").append(URLEncoder.encode("Login","UTF-8"));
            loginParametersBuilder.append("do=").append(URLEncoder.encode("loginForm-submit","UTF-8"));
            String loginParameters = loginParametersBuilder.toString();
            byte[] loginContent = loginParameters.getBytes("US-ASCII");

            URL loginDialog = new URL(loginDialogAddress);
            HttpURLConnection loginConnection = (HttpURLConnection)loginDialog.openConnection();           ;
            loginConnection.setDoOutput(true);
            loginConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
            loginConnection.setFixedLengthStreamingMode(loginContent.length);
            loginConnection.getOutputStream().write(loginParameters.getBytes("US-ASCII"));

            if (loginConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                StringBuilder allowParametersBuilder = new StringBuilder();
                allowParametersBuilder.append("&allow=").append(URLEncoder.encode(" ","UTF-8"));
                allowParametersBuilder.append("do=").append(URLEncoder.encode("allowForm-submit","UTF-8"));

                String allowParameters = allowParametersBuilder.toString();
                byte[] allowContent = allowParameters.getBytes("US-ASCII");
                String temp = "http://dev.senosec.czu.cz/api/user/allow?oauth_token="+consumer.getToken();
                URL allowDialog = new URL(temp);
                HttpURLConnection allowConnection = (HttpURLConnection)allowDialog.openConnection();

                allowConnection.setInstanceFollowRedirects(false);
                allowConnection.setDoOutput(true);
                allowConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                allowConnection.setFixedLengthStreamingMode(allowContent.length);
                allowConnection.getOutputStream().write(allowContent);

                error = Integer.toString(allowConnection.getResponseCode());
                if (allowConnection.getResponseCode() == 303) {
                    StringBuilder allowParametersBuilder2 = new StringBuilder();
                    allowParametersBuilder2.append("&allow=").append(URLEncoder.encode(" ","UTF-8"));
                    allowParametersBuilder2.append("do=").append(URLEncoder.encode("allowForm-submit","UTF-8"));

                    String allowParameters2 = allowParametersBuilder2.toString();
                    byte[] allowContent2 = allowParameters2.getBytes("US-ASCII");
                    String temp2 = "http://dev.senosec.czu.cz/api/user/allowed?oauth_token="+consumer.getToken();
                    URL allowDialog2 = new URL(temp2);
                    HttpURLConnection allowConnection2 = (HttpURLConnection)allowDialog.openConnection();

                    allowConnection2.setInstanceFollowRedirects(false);
                    allowConnection2.setDoOutput(true);
                    allowConnection2.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                    allowConnection2.setFixedLengthStreamingMode(allowContent.length);
                    allowConnection2.getOutputStream().write(allowContent2);

                    if (allowConnection2.getResponseCode() == 303){
                        String target2 = allowConnection2.getHeaderField("Location");
                        error = target2;
                    }
                    //String target = allowConnection.getHeaderField("Location");
                    //Uri targetUri = Uri.parse(target);
                    //String verifier = targetUri.getQueryParameter("oauth_verifier");
                    //error = target;
                    //provider.retrieveAccessToken(consumer, verifier);
                    login = false;
                }
            }

        } catch (IOException e) {
            error = e.getMessage();
            e.printStackTrace();
        } catch (OAuthNotAuthorizedException e) {
            error = e.getMessage();
            e.printStackTrace();
        } catch (OAuthCommunicationException e) {
            error = e.getMessage();
            e.printStackTrace();
        } catch (OAuthExpectationFailedException e) {
            error = e.getMessage();
            e.printStackTrace();
        } catch (OAuthMessageSignerException e) {
            error = e.getMessage();
            e.printStackTrace();
        }

    }
    }
4

0 回答 0