Use the afnetworking implementation to get Access-token in Basic authentication mode

Source: Internet
Author: User
Tags base64

Because the server side restricts the call API to get the data interface, we need to get a access-token before calling the API, so we need to implement this Access-token function in iOS. The server side is the use of the client Credentials Grand Way to issue tokens in ASP. Owin OAuth based on the implementation details of this blog.

According to the test code provided by the service side, the following:

Private AsyncTask Getaccesstoken (stringGranttype,stringUserName =NULL,stringPassword =NULL)  {    varParameters =Newdictionary<string,string>(); Parameters. ADD ("client_id","1234"); Parameters. ADD ("Client_secret","5678"); varClientId ="1234"; varClientsecret ="5678"; varParameters =Newdictionary<string,string>(); Parameters. ADD ("Grant_type", Granttype); if(!string. IsNullOrEmpty (userName) &&!string. IsNullOrEmpty (password)) {parameters. ADD ("username", UserName); Parameters. ADD ("Password", password); } _httpclient.defaultrequestheaders.authorization=NewAuthenticationheadervalue ("Basic", Convert.tobase64string (Encoding.ASCII.GetBytes (clientId+":"+Clientsecret))        ); varResponse =await_httpclient.postasync ("/token",Newformurlencodedcontent (parameters)); varResponsevalue =awaitResponse. Content.readasstringasync ();}

The code above shows that there are a few things that the client needs to do:

1, the client_id and Client_secret base64string encoding, in iOS has a special NSString BASE64 encoding Library, for details, see nsdata+base64.

2, the Grant_type for "client_credentials" as the parameter post to the server address;

3, the last note is afnetworking to achieve basic verification, when the value of the note when the basic has a space, and then set Content-type as "application/x-www-form-urlencoded; Charset=utf-8 ". So we can get access_token. The code is as follows:

Afhttprequestoperationmanager *manager =[Afhttprequestoperationmanager Manager]; NSString*client_id =@"1234"; NSString*client_secret =@"5678"; NSString*AUTHSTR = [NSString stringWithFormat:@"%@:%@", Client_id,client_secret]; NSData*authdata =[Authstr datausingencoding:nsutf8stringencoding]; NSString*authvalue = [NSString stringWithFormat:@"Basic%@", [Authdata base64encodedstringwithoptions:0]]; [Manager.requestserializer setvalue:authvalue Forhttpheaderfield:@"Authorization"]; [Manager.requestserializer SetValue:@"Application/x-www-form-urlencoded;charset=utf-8"Forhttpheaderfield:@"Content-type"]; Nsdictionary*params= @{@"Grant_type":@"client_credentials"}; [Manager POST:@"Http://api.cnblogs.com/token"Parametersparamssuccess:^ (Afhttprequestoperation *operation,IDresponseobject) {NSLog (@"JSON:%@", responseobject);} Failure:^ (Afhttprequestoperation *operation, Nserror *error) {NSLog (@"Error:%@", [error description]);}];

This completes the need for this scenario. The experiment obtains the Access_token in the Basic authentication way. This allows you to call the public API list after you have authorized the server.

Use the afnetworking implementation to get Access-token in Basic authentication mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.