我正在使用RestAssured库来自动化NetSuite Restlets。此Restlets使用OAuth 1.0进行身份验证。除了使用者密钥,使用者密钥,访问令牌和令牌密钥之外,我还需要设置高级字段,例如REALM。但是我找不到在RestAssured中设置它的任何方法。

RequestSpecification request = new RequestSpecBuilder()
                    .addHeader("Content-Type", ContentType.JSON.toString())
                    .setBaseUri(url).build()
                    .auth().oauth(
                          netsuiteConfig.getNetsuiteConsumerKey(),
                          netsuiteConfig.getNetsuiteConsumerSecret(),
                          netsuiteConfig.getNetsuiteTokenId(),
                          netsuiteConfig.getNetsuiteTokenSecret()
                     );


这是使用邮递员的api调用

java - 如何在RestAssured中设置OAuth领域-LMLPHP

最佳答案

RestAssured不支持此功能。使用某些库(我已经使用过com.github.seratch:signedrequest4j)创建OAuth 1.0字符串,并在RestAssured Authorization中设置RequestSpecification标头。

OAuthConsumer consumer = new OAuthConsumer(consumerKey, consumerSecret);
OAuthAccessToken accessToken = new OAuthAccessToken(tokenId, tokenSecret);

OAuthRealm realm = new OAuthRealm(myRealm);
SignedRequest request =
                   SignedRequestFactory.create(realm, consumer, accessToken);
request.readQueryStringAndAddToSignatureBaseString(url);
request.setHeader("Content-Type", "application/json");

String oAuthNonce = String.valueOf((new SecureRandom()).nextLong());
Long oAuthTimestamp = System.currentTimeMillis() / 1000L;
String signature = request.getSignature(url,
                   HttpMethod.POST, oAuthNonce, oAuthTimestamp);

String authorizationHeader = request
             .getAuthorizationHeader(signature, oAuthNonce, oAuthTimestamp);

09-28 14:04