我是REST API自动化的新手。我已经尝试了一个OAuth 2.0的代码。我想我已经按照指示编写了代码:(。之后我发现了错误。我已经了解了错误的原因,但找不到解决方法。请尽早帮我。
代码:
package OAUTH2;
import io.restassured.path.json.JsonPath;
import static io.restassured.RestAssured.given;
public class oauth {
public static void main(String args[])
{
String url="https://rahulshettyacademy.com/getCourse.php?code=4%2F1wHMlV4O0j9tHOEpL2WJodCPnkJF3cYv2b7mMTT2SAgkXQ8HHBlGp4J-nYyQolKOPi3_aeTxd5otQfVwfMMq9qY&scope=email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+openid&authuser=2&prompt=consent#";
String partialCode=url.split("code=")[1];
String code=partialCode.split("&scope")[0];
System.out.println(code);
String accessTokenResponse=given().urlEncodingEnabled(false)
.queryParam("code",code)
.queryParam("client_id", "692183103107-p0m7ent2hk7suguv4vq22hjcfhcr43pj.apps.googleusercontent.com")
.queryParam("client_secret", "erZOWM9g3UtwNRj340YYaK_W")
.queryParam("redirect uri", "https://rahulshettyacademy.com/getCourse.php")
.queryParam("grant-type", "authorization_code").
when().log().all()
.post("https://www.googleapis.com/oauth2/v4/token").asString();
JsonPath js=new JsonPath(accessTokenResponse);
String accessToken=js.getString(accessTokenResponse);
String Response=given()
.queryParam("access_token", accessToken)
.when().log().all()
.get("https://rahulshettyacademy.com/getCourse.php").asString();
System.out.println(Response);
}
}
错误:
4%2F1wHMlV4O0j9tHOEpL2WJodCPnkJF3cYv2b7mMTT2SAgkXQ8HHBlGp4J-nYyQolKOPi3_aeTxd5otQfVwfMMq9qY
Exception in thread "main" java.lang.IllegalArgumentException: Illegal character in query at index 244: /oauth2/v4/token?code=4%2F1wHMlV4O0j9tHOEpL2WJodCPnkJF3cYv2b7mMTT2SAgkXQ8HHBlGp4J-nYyQolKOPi3_aeTxd5otQfVwfMMq9qY&client_id=692183103107-p0m7ent2hk7suguv4vq22hjcfhcr43pj.apps.googleusercontent.com&client_secret=erZOWM9g3UtwNRj340YYaK_W&redirect uri=https://rahulshettyacademy.com/getCourse.php&grant-type=authorization_code
at java.net.URI.create(Unknown Source)
at java.net.URI.resolve(Unknown Source)
at io.restassured.internal.http.URIBuilder.setQueryNVP(URIBuilder.java:174)
at io.restassured.internal.http.URIBuilder.addQueryParams(URIBuilder.java:287)
at io.restassured.internal.http.URIBuilder.addQueryParams(URIBuilder.java:317)
at io.restassured.internal.http.URIBuilder$addQueryParams$0.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:139)
at io.restassured.internal.RequestSpecificationImpl.generateRequestUriForLogging(RequestSpecificationImpl.groovy:1119)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:107)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:323)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1262)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1029)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:816)
at groovy.lang.GroovyObject.invokeMethod(GroovyObject.java:39)
at org.codehaus.groovy.runtime.callsite.PogoInterceptableSite.call(PogoInterceptableSite.java:45)
at org.codehaus.groovy.runtime.callsite.PogoInterceptableSite.callCurrent(PogoInterceptableSite.java:55)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:51)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:171)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:194)
at io.restassured.internal.RequestSpecificationImpl.newFilterContext(RequestSpecificationImpl.groovy:1064)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:107)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:323)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1262)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1029)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:816)
at groovy.lang.GroovyObject.invokeMethod(GroovyObject.java:39)
at org.codehaus.groovy.runtime.callsite.PogoInterceptableSite.call(PogoInterceptableSite.java:45)
at org.codehaus.groovy.runtime.callsite.PogoInterceptableSite.callCurrent(PogoInterceptableSite.java:55)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:51)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:171)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:203)
at io.restassured.internal.RequestSpecificationImpl.applyPathParamsAndSendRequest(RequestSpecificationImpl.groovy:1653)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:107)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:323)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1262)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1029)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:816)
at groovy.lang.GroovyObject.invokeMethod(GroovyObject.java:39)
at org.codehaus.groovy.runtime.callsite.PogoInterceptableSite.call(PogoInterceptableSite.java:45)
at org.codehaus.groovy.runtime.callsite.PogoInterceptableSite.callCurrent(PogoInterceptableSite.java:55)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:51)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:171)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:203)
at io.restassured.internal.RequestSpecificationImpl.applyPathParamsAndSendRequest(RequestSpecificationImpl.groovy:1661)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:107)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:323)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1262)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1029)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:816)
at groovy.lang.GroovyObject.invokeMethod(GroovyObject.java:39)
at org.codehaus.groovy.runtime.callsite.PogoInterceptableSite.call(PogoInterceptableSite.java:45)
at org.codehaus.groovy.runtime.callsite.PogoInterceptableSite.callCurrent(PogoInterceptableSite.java:55)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:51)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:171)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:203)
at io.restassured.internal.RequestSpecificationImpl.post(RequestSpecificationImpl.groovy:175)
at io.restassured.internal.RequestSpecificationImpl.post(RequestSpecificationImpl.groovy)
at OAUTH2.oauth.main(oauth.java:23)
Caused by: java.net.URISyntaxException: Illegal character in query at index 244: /oauth2/v4/token?code=4%2F1wHMlV4O0j9tHOEpL2WJodCPnkJF3cYv2b7mMTT2SAgkXQ8HHBlGp4J-nYyQolKOPi3_aeTxd5otQfVwfMMq9qY&client_id=692183103107-p0m7ent2hk7suguv4vq22hjcfhcr43pj.apps.googleusercontent.com&client_secret=erZOWM9g3UtwNRj340YYaK_W&redirect uri=https://rahulshettyacademy.com/getCourse.php&grant-type=authorization_code
at java.net.URI$Parser.fail(Unknown Source)
at java.net.URI$Parser.checkChars(Unknown Source)
at java.net.URI$Parser.parseHierarchical(Unknown Source)
at java.net.URI$Parser.parse(Unknown Source)
at java.net.URI.<init>(Unknown Source)
... 76 more
提前感谢。
一切似乎都很好,正确的键是grant_type
,而不是grant-type
。要实现这一点,请打开您在评论部分发布的URL,并使用凭据登录。使用凭据登录后,复制URL并将其粘贴到下面的代码中,然后运行代码
如果您需要自动化上述操作,请使用硒
String url = "https://rahulshettyacademy.com/getCourse.php?code=4%2F1wG8uUh3B4vyGubOoLuW9DQIT7tLhe9CGJog4X7AAoQC94szQuf9WDkdX1V-wVqF7yAlsS9xZdcRJ5eROkrjLmc&scope=email+openid+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email&authuser=0&prompt=none#";
String partialcode = url.split("code=")[1];
String code = partialcode.split("&scope")[0];
String accessTokenResponse = given().urlEncodingEnabled(false)
.queryParams("code", code)
.queryParams("client_id", "692183103107-p0m7ent2hk7suguv4vq22hjcfhcr43pj.apps.googleusercontent.com")
.queryParams("client_secret", "erZOWM9g3UtwNRj340YYaK_W")
.queryParams("redirect_uri", "https://rahulshettyacademy.com/getCourse.php")
.queryParams("grant_type", "authorization_code").when()
.post("https://www.googleapis.com/oauth2/v4/token").then().extract().asString();
JsonPath jp = new JsonPath(accessTokenResponse);
String access_token = jp.getString("access_token");
String response = given().queryParam("access_token", access_token).when()
.get("https://rahulshettyacademy.com/getCourse.php").asString();
System.out.println(response);
响应:
{
"instructor": "RahulShetty",
"url": "rahulshettycademy.com",
"services": "projectSupport",
"expertise": "Automation",
"courses": {
"webAutomation": [{
"courseTitle": "Selenium Webdriver Java",
"price": "50"
}, {
"courseTitle": "Cypress",
"price": "40"
},
{
"courseTitle": "Protractor",
"price": "40"
}
],
"api": [{
"courseTitle": "Rest Assured Automation using Java",
"price": "50"
},
{
"courseTitle": "SoapUI Webservices testing",
"price": "40"
}
],
"mobile": [{
"courseTitle": "Appium-Mobile Automation using Java",
"price": "50"
}]
},
"linkedIn": "https://www.linkedin.com/in/rahul-shetty-trainer/"
}