本文介绍了如何在 RestAssured 中设置 OAuth 领域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using RestAssured library for automating NetSuite Restlets. This Restlets are using OAuth 1.0 for authentication. Apart from consumer key, consumer secret, access token and token secret, I need to set advanced fields like REALM. But I couldn't find any way to set that in RestAssured.

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

这里是使用 Postman 的 api 调用

Here is the api call using Postman

推荐答案

我正在使用上一个答案中提到的库,但后来我意识到我需要使用不受支持的 PATCH 请求.

I was using the library mentioned in the previous answer but then I realised I needed to use PATCH requests which wasn't supported.

经过几天的尝试,我开始使用 google oauth 客户端,终于让这个例子工作了:

I started using the google oauth client instead and after days of trying, finally got this example working:

val signer = OAuthHmacSigner()
signer.clientSharedSecret = CONSUMER_SECRET
signer.tokenSharedSecret = TOKEN_SECRET

val oauthParameters = OAuthParameters()
oauthParameters.consumerKey = CONSUMER_KEY
oauthParameters.token = ACCESS_TOKEN
oauthParameters.signer = signer
val genericUrl = GenericUrl("https://{ACC_ID}.suitetalk.api.netsuite.com/path/to/endpoint")
oauthParameters.version = "1.0"
oauthParameters.computeNonce()
oauthParameters.computeTimestamp()
oauthParameters.computeSignature("GET", genericUrl)

oauthParameters.realm = REALM
val authHeader = oauthParameters.authorizationHeader

RestAssured.with()
        .log().all()
        .header("Authorization", authHeader)
        .urlEncodingEnabled(false)
        .request(Method.GET, genericUrl.toString())
        .then()
        .statusCode(200)

urlEncoding 对于查询参数已经编码的 url 设置为 false.例如:{url}/invoice?q=internalid%20IS%2012

urlEncoding is set to false for urls with query params that are already encoded. For example: {url}/invoice?q=internalid%20IS%2012

我希望它对未来的人有所帮助!

I hope it helps someone in the future!

这篇关于如何在 RestAssured 中设置 OAuth 领域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 04:23