-d 和 -u 参数的含义(和提供方法)是什么?
What's the meaning (and method to provide) the -d and -u arguments?
我正在尝试使用 Scala 中的 Playframework 进行 WS GET 调用以调用 Paypal REST JSON API。我更具体地尝试 get the initial Paypal access token:
curl -v https://api.sandbox.paypal.com/v1/oauth2/token \
-H "Accept: application/json" \
-H "Accept-Language: en_US" \
-u "client_id:secret" \
-d "grant_type=client_credentials"
我通过以下方式在 Scala 中构建它:
@Inject(ws: WSClient)
val url = config.get[String]("paypal.url.token") // https://api.sandbox.paypal.com/v1/oauth2/token
val httpHeaders = Array(
"Accept" -> "application/json",
"Accept-Language" -> "en_US"
)
val username = config.get[String]("paypal.client_id")
val password = config.get[String]("paypal.secret")
val request: WSRequest = ws.url(url).withHttpHeaders(httpHeaders: _*).
withRequestTimeout(timeout).
withAuth(username, password, WSAuthScheme.BASIC)
val futureResponse = request.get()
futureResponse.map { response =>
println(response.json)
}
我在这里假设原始 curl
样本中的 -u
意味着并对应于 withAuth
但我不知道 -d
对 grant_type=client_credentials
对应?
当我 运行 它像现在一样时,我收到以下错误:
{"error":"invalid_token","error_description":"Authorization header does not have valid access token"}
还有一些在日志之前:
[info] p.s.a.o.a.n.h.i.Unauthorized401Interceptor - Can't handle 401 as auth was already performed
您可以使用 man curl
阅读 curl
手册。
所以,
1) -u
代表--user <username:passsword>
-u, --user <user:password>
Specify the user name and password to use for server authentication. Overrides -n, --netrc and --netrc-optional.
If you simply specify the user name, curl will prompt for a password.
这也转化为-H "Authorization: <Basic|Bearer> base64_for_username:password"
2) -d
表示 --data
或 POST 请求上的负载。
-d, --data <data>
(HTTP) Sends the specified data in a POST request to the HTTP server, in the same way that a browser does when a user has
filled in an HTML form and presses the submit button. This will cause curl to pass the data to the server using the con-
tent-type application/x-www-form-urlencoded. Compare to -F, --form.
- 验证
withAuth(username, password, WSAuthScheme.BASIC)
是否使用正确的用户名、密码
- 并在代码中缺少的 scala 中设置 post 主体。我不太了解游戏框架,但这可能是正确的方式 - https://www.playframework.com/documentation/2.6.3/api/java/play/libs/ws/WSRequest.html#post-java.lang.String-
尝试 post
而不是像这样 get
ws
.url(url)
.withAuth(username, password, WSAuthScheme.BASIC)
.post(Map("grant_type" -> Seq("client_credentials")))
.map(println)
我正在尝试使用 Scala 中的 Playframework 进行 WS GET 调用以调用 Paypal REST JSON API。我更具体地尝试 get the initial Paypal access token:
curl -v https://api.sandbox.paypal.com/v1/oauth2/token \
-H "Accept: application/json" \
-H "Accept-Language: en_US" \
-u "client_id:secret" \
-d "grant_type=client_credentials"
我通过以下方式在 Scala 中构建它:
@Inject(ws: WSClient)
val url = config.get[String]("paypal.url.token") // https://api.sandbox.paypal.com/v1/oauth2/token
val httpHeaders = Array(
"Accept" -> "application/json",
"Accept-Language" -> "en_US"
)
val username = config.get[String]("paypal.client_id")
val password = config.get[String]("paypal.secret")
val request: WSRequest = ws.url(url).withHttpHeaders(httpHeaders: _*).
withRequestTimeout(timeout).
withAuth(username, password, WSAuthScheme.BASIC)
val futureResponse = request.get()
futureResponse.map { response =>
println(response.json)
}
我在这里假设原始 curl
样本中的 -u
意味着并对应于 withAuth
但我不知道 -d
对 grant_type=client_credentials
对应?
当我 运行 它像现在一样时,我收到以下错误:
{"error":"invalid_token","error_description":"Authorization header does not have valid access token"}
还有一些在日志之前:
[info] p.s.a.o.a.n.h.i.Unauthorized401Interceptor - Can't handle 401 as auth was already performed
您可以使用 man curl
阅读 curl
手册。
所以,
1) -u
代表--user <username:passsword>
-u, --user <user:password> Specify the user name and password to use for server authentication. Overrides -n, --netrc and --netrc-optional. If you simply specify the user name, curl will prompt for a password.
这也转化为-H "Authorization: <Basic|Bearer> base64_for_username:password"
2) -d
表示 --data
或 POST 请求上的负载。
-d, --data <data> (HTTP) Sends the specified data in a POST request to the HTTP server, in the same way that a browser does when a user has filled in an HTML form and presses the submit button. This will cause curl to pass the data to the server using the con- tent-type application/x-www-form-urlencoded. Compare to -F, --form.
- 验证
withAuth(username, password, WSAuthScheme.BASIC)
是否使用正确的用户名、密码 - 并在代码中缺少的 scala 中设置 post 主体。我不太了解游戏框架,但这可能是正确的方式 - https://www.playframework.com/documentation/2.6.3/api/java/play/libs/ws/WSRequest.html#post-java.lang.String-
尝试 post
而不是像这样 get
ws
.url(url)
.withAuth(username, password, WSAuthScheme.BASIC)
.post(Map("grant_type" -> Seq("client_credentials")))
.map(println)