使用JMeter中的Oauth2向Google进行身份验证

时间:2017-03-14 03:47:07

标签: oauth google-drive-api jmeter google-oauth google-oauth2

我正在进行POST调用以在JMeter中获取oAuth令牌。这样我就可以将文件上传到Google云端硬盘了。以下是JMeter中的请求详细信息。

POST https://accounts.google.com/o/oauth2/token

POST data:
client_id=<my_client_id>&auth_uri=https%3A%2F%2Faccounts.google.com%2Fo%2Foauth2%2Fauth&token_uri=https%3A%2F%2Faccounts.google.com%2Fo%2Foauth2%2Ftoken&client_secret=<my_client_secret>&grant_type=authorization_code&redirect_uris=%5B%22urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob%22%2C+%22127.0.0.1%3A3000%22%5D%0A&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive

[no cookies]

Request Headers:
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded
Content-Length: 424
Host: accounts.google.com
User-Agent: Apache-HttpClient/4.5.2 (Java/1.8.0_121)

但我收到了以下错误。任何帮助表示赞赏。感谢。

{
:   
  "error" 
:   : "invalid_request",

:   "error_description" : "Missing required parameter: code"

}

2 个答案:

答案 0 :(得分:0)

我建议使用Google OAuth Java Client Library JSR223 Test Elements使用Groovy作为编程语言,这是获取/刷新OAuth令牌的最快,最简单的方法。

有关详细说明和授权示例,请参阅How to Run Performance Tests on OAuth Secured Apps with JMeter文章。

答案 1 :(得分:0)

我无法帮助您使用JMeter,但我可以告诉您错误消息的含义。

grant_type=authorization_code是Oauth2流程的第二步。它有一些必需的参数,其中一个是代码。

https://accounts.google.com/o/oauth2/token
code=4/X9lG6uWd8-MMJPElWggHZRzyFKtp.QubAT_P-GEwePvB8fYmgkJzntDnaiAI&client_id={ClientId}.apps.googleusercontent.com&client_secret={ClientSecret}&redirect_uri=urn:ietf:wg:oauth:2.0:oob&grant_type=authorization_code

有问题的代码是用户批准应用程序访问的初始请求返回的代码。

我还想补充一点,Google有许多官方客户端库,可以为您处理这些电话。使用其中一个更容易,然后尝试理解Oauth2流程,如果你不需要。如果您无法使用该库,并且您有兴趣看到完整的Oauth2流程到谷歌我有一个教程Google 3 legged Oauth2 flow它显示纯HTTP调用。