如何通过REST接口获取token
/*--------------------------获取管理员oken---------------------------------------*/
NSURL *url = [NSURL URLWithString:@"https://a1.easemob.com/企业ID/应用app名称/token"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"POST";
NSDictionary *body = @{@"grant_type":@"client_credentials",@"client_id": @"填写Client Id",@"client_secret":@"填写Client Secret"};
[request setHTTPBody:[body JSONData]];//JSONData为一个库的方法,该库在下面附件中
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
//连接,异步
[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (connectionError == nil) {
id obj = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSLog(@"服务器返回信息%@",obj);
NSString* token = [obj objectForKey:@"access_token"];
if(token)
{
// access_token 保存一下(可以把token保存到本地)
[[NSUserDefaults standardUserDefaults] setObject:token forKey:@"access_token"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
}
NSLog(@"错误信息%@",connectionError);
}];
NSURL *url = [NSURL URLWithString:@"https://a1.easemob.com/企业ID/应用app名称/token"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"POST";
NSDictionary *body = @{@"grant_type":@"client_credentials",@"client_id": @"填写Client Id",@"client_secret":@"填写Client Secret"};
[request setHTTPBody:[body JSONData]];//JSONData为一个库的方法,该库在下面附件中
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
//连接,异步
[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (connectionError == nil) {
id obj = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSLog(@"服务器返回信息%@",obj);
NSString* token = [obj objectForKey:@"access_token"];
if(token)
{
// access_token 保存一下(可以把token保存到本地)
[[NSUserDefaults standardUserDefaults] setObject:token forKey:@"access_token"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
}
NSLog(@"错误信息%@",connectionError);
}];