ios - How to fetch data and display using "GET"-with Authorization header using Rest API -


i having 1 url data , display. here need follow steps authauthorization header: base64,sha256 0r sha512

so here coded data authorization header.but in console got error:

str: {   "error_code": "invalid_auth_header",   "message": "invalid authorization. use auth header or access hash" } 

i new http/get, authorization header , all. doing wrong , need change there work out?

here code (this update code based on below answer):

 #import "viewcontroller.h"  #import <commoncrypto/commondigest.h>   @interface viewcontroller ()   @end   @implementation viewcontroller   - (void)viewdidload {       [super viewdidload];       nsstring *username = @"testingyouonit@gmail.com";       nsstring *password = @"testingyouonit2";        nsdata *plaindata = [password datausingencoding:nsutf8stringencoding];       nsstring *base64string = [plaindata base64encodedstringwithoptions:0];       base64string=[self sha256hashfor: base64string];        //setting string of url taking appliance ip.       nsstring *urlstring = @"https://api.exampleurl.com/files";        nsmutableurlrequest *request= [[nsmutableurlrequest alloc] init];       [request seturl:[nsurl urlwithstring:urlstring]];       [request sethttpmethod:@"get"];        nsstring *authstr = [nsstring stringwithformat:@"%@:%@", username, base64string];       nsdata *authdata = [authstr datausingencoding:nsutf8stringencoding];       nsstring *authvalue = [nsstring stringwithformat:@"basic %@", [authdata base64encoding]];       [request setvalue:authvalue forhttpheaderfield:@"authorization"];        nsdata *returndata = [nsurlconnection sendsynchronousrequest:request returningresponse:nil error:nil];        nsstring *str = [[nsstring alloc] initwithdata:returndata encoding:nsutf8stringencoding];        nslog(@"str: %@", str); } -(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; } 

first of all, please fix following error in code:

  1. change base64string=[self sha256hashfor: password]; base64string=[self sha256hashfor: base64string];.
  2. nsstring *str1 = [nsstring stringwithformat:@"https://api.exampleurl/user/data",username,base64string]; nsstring *str1 = [nsstring stringwithformat:@"https://api.exampleurl/user/data"];

after that, use basic authorization use following code:

nsstring *authstr = [nsstring stringwithformat:@"%@:%@", username, base64string]; nsdata *authdata = [authstr datausingencoding:nsutf8stringencoding]; nsstring *authvalue = [nsstring stringwithformat:@"basic %@", [authdata base64encoding]]; [request setvalue:authvalue forhttpheaderfield:@"authorization"]; 

Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -