Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Objective-C) AzureWebsites OAuth2 Password FlowDemonstrates how to do OAuth 2.0 password flow for azurewebsites.net.
#import <CkoHttp.h> #import <CkoHttpRequest.h> #import <NSString.h> #import <CkoHttpResponse.h> #import <CkoStringBuilder.h> #import <CkoJsonObject.h> // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. CkoHttp *http = [[CkoHttp alloc] init]; BOOL success; CkoHttpRequest *req = [[CkoHttpRequest alloc] init]; req.HttpVerb = @"POST"; req.Path = @"/token"; req.ContentType = @"application/x-www-form-urlencoded"; [req AddParam: @"grant_type" value: @"password"]; [req AddParam: @"username" value: @"your_username"]; [req AddParam: @"password" value: @"your_password"]; NSString *tokenEndpoint = @"https://your_api.azurewebsites.net/token"; CkoHttpResponse *resp = [http PostUrlEncoded: tokenEndpoint req: req]; if (http.LastMethodSuccess == NO) { NSLog(@"%@",http.LastErrorText); return; } CkoStringBuilder *sbResponseBody = [[CkoStringBuilder alloc] init]; [resp GetBodySb: sbResponseBody]; CkoJsonObject *jResp = [[CkoJsonObject alloc] init]; [jResp LoadSb: sbResponseBody]; jResp.EmitCompact = NO; NSLog(@"%@",@"Response Body:"); NSLog(@"%@",[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.StatusCode intValue]; NSLog(@"%@%d",@"Response Status Code = ",respStatusCode); if (respStatusCode >= 400) { NSLog(@"%@",@"Response Header:"); NSLog(@"%@",resp.Header); NSLog(@"%@",@"Failed."); return; } // ---------------------------------- // Use the OAuth2 token in a request. // For example... CkoStringBuilder *sbXml = [[CkoStringBuilder alloc] init]; success = [sbXml LoadFile: @"c:/someDir/someXmlFile.xml" charset: @"utf-8"]; if (success == NO) { NSLog(@"%@",@"Failed to load the XML file."); return; } // Get the OAuth2 token and use it for authentication http.AuthToken = [jResp StringOf: @"token"]; NSString *destUrl = @"https://your_api.azurewebsites.net/destinationUrl"; resp = [http PostXml: destUrl xmlDoc: [sbXml GetAsString] charset: @"utf-8"]; if (http.LastMethodSuccess == NO) { NSLog(@"%@",http.LastErrorText); return; } respStatusCode = [resp.StatusCode intValue]; NSLog(@"%@%d",@"Response Status Code = ",respStatusCode); if (respStatusCode >= 400) { NSLog(@"%@",@"Response Header:"); NSLog(@"%@",resp.Header); NSLog(@"%@",@"Failed."); return; } // Examine the response body NSLog(@"%@",resp.BodyStr); |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.