0

I am new to REST API automation. I have tried one code for OAuth 2.0 . I have written the code as instructed, I guess :) . After that I found the error. I have understood the cause of the error but couldn't find resolution for the same. Please help me as early as possible.

Code :

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);
    }
    
}

Error :

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

Thanks in Advance.

  • Can you share the params encoded full URL – VP7 Jul 14 '20 at 17:51
  • @VP7... here it is.. https://accounts.google.com/o/oauth2/v2/auth?scope=https://www.googleapis.com/auth/userinfo.email&auth_url=https://accounts.google.com/o/oauth2/v2/auth&client_id=692183103107-p0m7ent2hk7suguv4vq22hjcfhcr43pj.apps.googleusercontent.com&response_type=code&redirect_uri=https://rahulshettyacademy.com/getCourse.php – Ansuman Prasad Pati Jul 15 '20 at 10:51
  • @AnsumanPrasadPati Check the answer and let me know – Wilfred Clement Jul 15 '20 at 13:54
  • @WilfredClement.. The answer is correct.. One thing I would like to inform you using selenium we can't automate as you have mentioned because Google has removed the facility. – Ansuman Prasad Pati Jul 16 '20 at 10:26

1 Answers1

0

Everything seems fine, the right key is grant_type and not grant-type, To get this working open the URL which you posted in the comment section and login with the credentials. After you login with the credentials copy the URL and paste it in the code below and just run the code

If you need to automate the above then use

    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);

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/"
}
Wilfred Clement
  • 2,674
  • 2
  • 14
  • 29