Sample code for 30+ languages & platforms
Java Requires Chilkat v11.0.0+

AzureWebsites OAuth2 Password Flow

See more OAuth2 Examples

Demonstrates how to do OAuth 2.0 password flow for azurewebsites.net.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpRequest;
import com.chilkatsoft.chilkat2.HttpResponse;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.StringBuilder;

public class ChilkatExample {

    public static void main(String[] args) {
        // This example requires the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        Http http = new Http();

        HttpRequest req = new HttpRequest();
        req.setHttpVerb("POST");
        req.setPath("/token");
        req.setContentType("application/x-www-form-urlencoded");
        req.addParam("grant_type", "password");
        req.addParam("username", "your_username");
        req.addParam("password", "your_password");

        String tokenEndpoint = "https://your_api.azurewebsites.net/token";

        HttpResponse resp = new HttpResponse();
        try {
            http.httpReq(tokenEndpoint, req, resp);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        StringBuilder sbResponseBody = new StringBuilder();
        resp.getBodySb(sbResponseBody);
        JsonObject jResp = new JsonObject();
        jResp.loadSb(sbResponseBody);
        jResp.setEmitCompact(false);

        System.out.println("Response Body:");
        System.out.println(jResp.emit());

        // Sample JSON response:

        // {
        //   "access_token": "NQGHn ... xTS",
        //   "token_type": "bearer",
        //   "expires_in": 1209599,
        //   "userName": "your_username",
        //   ".issued": "Mon, 27 Apr 2020 23:49:35 GMT",
        //   ".expires": "Mon, 11 May 2020 23:49:35 GMT"
        // }

        int respStatusCode = resp.getStatusCode();
        System.out.println("Response Status Code = " + respStatusCode);
        if (respStatusCode >= 400) {
            System.out.println("Response Header:");
            System.out.println(resp.getHeader());
            System.out.println("Failed.");
            return;
        }

        // ----------------------------------
        // Use the OAuth2 token in a request.
        // For example...

        StringBuilder sbXml = new StringBuilder();
        try {
            sbXml.loadFile("c:/someDir/someXmlFile.xml", "utf-8");
        } catch (ChilkatException e) {
            System.out.println("Failed to load the XML file.");
            return;
        }

        // Get the OAuth2 token and use it for authentication
        http.setAuthToken(jResp.stringOf("token"));

        String destUrl = "https://your_api.azurewebsites.net/destinationUrl";
        try {
            http.httpSb("POST", destUrl, sbXml, "utf-8", "application/xml", resp);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        respStatusCode = resp.getStatusCode();
        System.out.println("Response Status Code = " + respStatusCode);
        if (respStatusCode >= 400) {
            System.out.println("Response Header:");
            System.out.println(resp.getHeader());
            System.out.println("Failed.");
            return;
        }

        // Examine the response body
        System.out.println(resp.getBodyStr());
    }
}