ios - Basic auth header from username and password is not working In AFNetworking 2.0 -


i invoking api required authorisation header.

this api need on given url

username : testingyouonit@gmail.com

password : testingyouonit2

to create authorisation header

step 1 - base64 encoding of given password.

step 2 - sha256 hash on password obtained in step 1

step 3 - use password obtained in step 2 , given username create authorization header

now passing request using afnetworking

nsstring *email=@"testingyouonit@gmail.com"; nsstring *password=[self encodestringto64:@"testingyouonit2"]; 

here encoded password

- (nsstring*)encodestringto64:(nsstring*)fromstring {     nsdata *plaindata = [fromstring datausingencoding:nsutf8stringencoding];     nsstring *base64string;     if ([plaindata respondstoselector:@selector(base64encodedstringwithoptions:)]) {         base64string = [plaindata base64encodedstringwithoptions:kniloptions];     } else {         base64string = [plaindata base64encoding];     }       return base64string;  } 

now passing request

afhttprequestoperationmanager *manager = [afhttprequestoperationmanager manager];     manager.responseserializer = [afjsonresponseserializer serializer];      [manager.requestserializer setvalue:@"application/json" forhttpheaderfield:@"accept"];     [manager.requestserializer setvalue:@"application/json" forhttpheaderfield:@"content-type"];     manager.requestserializer = [afjsonrequestserializer serializer];     manager.requestserializer = [afhttprequestserializer serializer];     [manager.requestserializer setauthorizationheaderfieldwithusername:email password:password];     [manager.requestserializer setvalue:@"cas256" forhttpheaderfield:@"authorization"];       [manager get:@"https://myurlhere/youit" parameters:nil success:^(afhttprequestoperation *operation, id responseobject) {         nslog(@"json: %@", responseobject);     } failure:^(afhttprequestoperation *operation, nserror *error) {         nslog(@"error: %@", error);     }]; 

now each time getting

error: error domain=com.alamofire.error.serialization.response code=-1011 "request failed: bad request (400)" userinfo=0x7c361610 {com.alamofire.serialization.response.error.response=<nshttpurlresponse: 0x7aed9220> { url: https://myurlhere/youit } { status code: 400, headers {     "access-control-allow-origin" = "*";     connection = "keep-alive";     "content-length" = 114;     "content-type" = "application/json";     date = "sat, 11 jul 2015 02:56:46 gmt";     server = "nginx/1.1.19"; } }, nserrorfailingurlkey=https://myurlhere/youit, nslocalizeddescription=request failed: bad request (400), com.alamofire.serialization.response.error.data=<7b0a2020 22657272 6f725f63 6f646522 3a202269 6e76616c 69645f61 7574685f 68656164 6572222c 0a202022 6d657373 61676522 3a202249 6e76616c 69642061 7574686f 72697a61 74696f6e 2e205573 6520616e 20617574 68206865 61646572 206f7220 61636365 73732068 61736822 0a7d>} 

where mistake doing make basic auth

lets create sha256 , pass password , try

nsstring *email=@"testingyouonit@gmail.com"; nsstring *password=[self encodestringto64:@"testingyouonit2"]; 

add 1 more method generate sha256 password , pass in request

step-1 using method need #include <commoncrypto/commondigest.h>

-(nsstring*)sha256hashfor:(nsstring*)input {     const char* str = [input utf8string];     unsigned char result[cc_sha256_digest_length];     cc_sha256(str, strlen(str), result);      nsmutablestring *ret = [nsmutablestring stringwithcapacity:cc_sha256_digest_length*2];     for(int = 0; i<cc_sha256_digest_length; i++)     {         [ret appendformat:@"%02x",result[i]];     }     return ret; } 

and call this
note : pass here encoded password used

nsstring *password=[self encodestringto64:@"testingyouonit2"];  `password=[self sha256hashfor: password];` 

and final step

afhttprequestoperationmanager *manager = [afhttprequestoperationmanager manager];     manager.responseserializer = [afjsonresponseserializer serializer];      [manager.requestserializer setvalue:@"application/json" forhttpheaderfield:@"accept"];     [manager.requestserializer setvalue:@"application/json" forhttpheaderfield:@"content-type"];     manager.requestserializer = [afjsonrequestserializer serializer];     manager.requestserializer = [afhttprequestserializer serializer];     [manager.requestserializer setauthorizationheaderfieldwithusername:email password:password];        [manager get:@"https://myurlhere/youit" parameters:nil success:^(afhttprequestoperation *operation, id responseobject) {         nslog(@"json: %@", responseobject);     } failure:^(afhttprequestoperation *operation, nserror *error) {         nslog(@"error: %@", error);     }]; 

let me see result


Comments

Popular posts from this blog

java - Andrioid studio start fail: Fatal error initializing 'null' -

android - Gradle sync Error:Configuration with name 'default' not found -

StringGrid issue in Delphi XE8 firemonkey mobile app -