Vishal
Vishal

Reputation: 127

How to programatically create and get Web Apps under App Services in Windows Azure?

I would like to programatically get and create Web Apps under App Services in Windows Azure.

How to create it by using its REST API?

Upvotes: 0

Views: 67

Answers (1)

Brando Zhang
Brando Zhang

Reputation: 28057

As far as I know, if you want to use REST API to get and create Web App, you need generate a access token. To generate access token we need talentID, clientId, clientSecret. We need to assign application to role, after that then we can use token in common. More information about how to assign application to role please refer to the article .This blog also has more detail steps to get AceessToken.

Notice: I use C# Codes to show a example.

Then you could send request as below:

Get webapp:

Url: https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroup}/Microsoft.Web/sites?api-version={api-version}

Method: PUT

Parameter:
subscriptionId  The identifier of your subscription where the snapshot is being created.
resourceGroup   The name of the resource group that will contain the snapshot.
api-version The version of the API to use.

The codes is as below:

  // Display the file contents to the console. Variable text is a string.
    string tenantId = "xxxxxxxxxxxxxxxxxxxxx";
    string clientId = "xxxxxxxxxxxxxxxxxxxxx";
    string clientSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxx";
    string subscriptionid = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    string resourcegroup = "BrandoSecondTest";


    string version = "2015-08-01";
    string authContextURL = "https://login.windows.net/" + tenantId;
    var authenticationContext = new AuthenticationContext(authContextURL);
    var credential = new ClientCredential(clientId, clientSecret);
    var result = authenticationContext.AcquireTokenAsync(resource: "https://management.azure.com/", clientCredential: credential).Result;
    if (result == null)
    {
        throw new InvalidOperationException("Failed to obtain the JWT token");
    }
    string token = result.AccessToken;
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(string.Format("https://management.azure.com/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Web/sites?api-version={2}", subscriptionid, resourcegroup, version));
    request.Method = "GET";
    request.Headers["Authorization"] = "Bearer " + token;

    request.ContentType = "application/json";
    var httpResponse = (HttpWebResponse)request.GetResponse();
    using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
    {
        Console.WriteLine(streamReader.ReadToEnd());
    }

    Console.ReadLine();

Create WebApp:

You should send your webapp template in the request content.

Then you could send request as below:

Url: https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroup}/Microsoft.Web/sites/{appname}?api-version={api-version}

Method: PUT

Parameter:
subscriptionId  The identifier of your subscription where the snapshot is being created.
resourceGroup   The name of the resource group that will contain the snapshot.
appname:  The name of your webapplicaiton you want to create
api-version The version of the API to use.

Request content:

{
  "location": "East Asia",
  "properties": {
    "name": "YourWebpplcaitonName",
    "serverFarmId": "Yourserviceplan"
  }
}

Here is a C# example, hope it gives some tips:

string body = File.ReadAllText(@"D:\json.txt");
            // Display the file contents to the console. Variable text is a string.
            string tenantId = "xxxxxxxxxxxxxxxxxxxxxxxxx";
            string clientId = "xxxxxxxxxxxxxxxxxxxxxxx";
            string clientSecret = "xxxxxxxxxxxxxxxxxxxxxxxxx";
            string subscriptionid = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx";
            string resourcegroup = "BrandoSecondTest";
            string appname = "Brandosss";

            string version = "2015-08-01";
            string authContextURL = "https://login.windows.net/" + tenantId;
            var authenticationContext = new AuthenticationContext(authContextURL);
            var credential = new ClientCredential(clientId, clientSecret);
            var result = authenticationContext.AcquireTokenAsync(resource: "https://management.azure.com/", clientCredential: credential).Result;
            if (result == null)
            {
                throw new InvalidOperationException("Failed to obtain the JWT token");
            }
            string token = result.AccessToken;
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(string.Format("https://management.azure.com/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Web/sites/{2}?api-version={3}", subscriptionid, resourcegroup,appname, version));
            request.Method = "PUT";
            request.Headers["Authorization"] = "Bearer " + token;
            request.ContentLength = body.Length;
            request.ContentType = "application/json";
            try
            {
                using (var streamWriter = new StreamWriter(request.GetRequestStream()))
                {
                    streamWriter.Write(body);
                    streamWriter.Flush();
                    streamWriter.Close();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            // Get the response
            var httpResponse = (HttpWebResponse)request.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                Console.WriteLine(streamReader.ReadToEnd());
            }

Upvotes: 2

Related Questions