net.oauth.OAuthProblemException:signature_invalid

时间:2018-08-03 11:51:13

标签: java javafx server

我正在尝试与本地服务器建立连接,但是身份验证出现问题。它说:

OAuth身份验证失败! net.oauth.OAuthProblemException:signature_invalid oauth_problem:signature_invalid

我的代码有什么问题,我必须手动定义签名吗?如果是,我应该在指令中输入哪个值 message.addParameter(OAuth.OAUTH_SIGNATURE,“值??”); 感谢任何帮助。

public class Main extends Application {
    @Override
    public void start(Stage stage) {

        String matrikelnummer = "1111";
        String urlstring = "http://localhost:8080/jack2/moodleServlet";
        String encodedHash="";
        String consumerkey = "test";
        String secret = "123456";


        try {       

            HttpPost httppost = new HttpPost(urlstring);

            List<NameValuePair> args = new ArrayList<NameValuePair>();
            args.add(new BasicNameValuePair("lti_message_type", "basic-lti-launch-request"));
            args.add(new BasicNameValuePair("lti_version", "LTI-1p0"));

            args.add(new BasicNameValuePair("user_name", matrikelnummer));
            args.add(new BasicNameValuePair("custom_jackexerciseid", "12"));
            args.add(new BasicNameValuePair("lis_result_sourcedid", "154543564653564"));
            args.add(new BasicNameValuePair("resource_link_id", "1676576575765"));

            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(args);

            MessageDigest digest = MessageDigest.getInstance("SHA-1");
            digest.reset();
            encodedHash = Base64.getEncoder().encodeToString(digest.digest(args.toString().getBytes("UTF-8")));

            //encodedHash = Base64.Encoder(digest.digest(args.toString().getBytes("UTF-8")));
            String nonce = UUID.randomUUID().toString();
            String timestamp = Long.toString(TimeUnit.MILLISECONDS.toSeconds( System.currentTimeMillis()));
            OAuthMessage message = new OAuthMessage(OAuthMessage.POST, urlstring, null, entity.getContent());
            message.addParameter(OAuth.OAUTH_CONSUMER_KEY, consumerkey);
            message.addParameter(OAuth.OAUTH_SIGNATURE_METHOD, OAuth.HMAC_SHA1);
            //message.addParameter(OAuth.OAUTH_SIGNATURE, "value??");

            message.addParameter(OAuth.OAUTH_TIMESTAMP, timestamp);
            message.addParameter(OAuth.OAUTH_NONCE, nonce);
            message.addParameter("oauth_body_hash", encodedHash);
            message.addParameter("oauth_body_hash", encodedHash);

            OAuthConsumer consumer = new OAuthConsumer(null, consumerkey, secret, null);
            OAuthAccessor accessor = new OAuthAccessor(consumer);

            message.sign(accessor);
            httppost.setEntity(entity);

            String OAUTH_HEADER = "OAuth ";

                for(Entry<String,String> h : message.getParameters()) {
                    if(!OAUTH_HEADER.isEmpty() && !OAUTH_HEADER.endsWith(","))
                        OAUTH_HEADER +=",";
                    OAUTH_HEADER += h.getKey() + "=" +"\"" +URLEncoder.encode(h.getValue(), "UTF-8")+"\"";
                }

                httppost.addHeader("Authorization",OAUTH_HEADER);

                Header[] h1 = httppost.getAllHeaders();
                for(int i=0; i<h1.length; i++) {
                    System.out.println("HeaderRequest: "+ h1[i].getName() + "----- Wert: " + h1[i].getValue());
                }


            HttpClientBuilder clientb = HttpClientBuilder.create();
            clientb.setRedirectStrategy(new LaxRedirectStrategy());
            HttpClient client = clientb.build();
            HttpResponse response = client.execute(httppost);

            InputStream is = response.getEntity().getContent();

            byte[] buffer = new byte[1024];
            StringBuilder sb = new StringBuilder();
            int length =0;
            while((length = is.read(buffer))>=0) {
                sb.append(new String(Arrays.copyOfRange(buffer, 0, length), "UTF-8"));
            }



            WebView web = new WebView();
            WebEngine engine = web.getEngine();
            engine.loadContent(sb.toString());


            Scene scene = new Scene(web);
            stage.setScene(scene);
            stage.show();

        }catch(Exception e) {
            throw new RuntimeException(e);
        }

    }


    public static void main(String[] args) {
        launch(args);
    }
}

0 个答案:

没有答案