ski*_*atg 1 networking post oauth objective-c ios
我正在添加使用oauth2在iOS 7应用程序中在LinkedIn上分享文章的功能.我已经通过身份验证并拥有访问令牌.文档似乎很清楚,但实际上发布的事情很奇怪,事情变得非常模糊和记录不清.我知道我在这里发帖:http://api.linkedin.com/v1/people/~/shares附加令牌.
iOS是一个巨大的平台,而且很受欢迎,我认为我错过了一些明显的东西,但很多谷歌搜索都揭示了旧项目的旧参考.一个例子使用oauth2并确实通过身份验证,但我不能进行任何api调用.我错过了本身的页面吗?我已经阅读了分享api页面,但是当谈到api调用时,使用objective-c并不是一个例子.我不打算什么都不做,但我只是对缺乏信息感到惊讶.
每个示例都使用OAMutableRequest,构建字典等相同的代码.但他们从不解释那是什么,如何整合那个图书馆或任何东西,它只是奇怪.这是公认的最佳实践,图书馆在3年内没有更新,因此它有弧形和其他东西的错误.所有代码示例都提到了相同的"消费者"属性,没有讨论如何或为什么需要这样做.我似乎无法找到如何使用参数linkedin构建post请求,以便在网站上发布内容.OAMutableRequest是唯一的方法吗?如果是这样,人们如何更新它的工作?如果没有,您如何使用NSURLRequest或更简单的方法构建请求.非常感谢!
要发布json数据,您可以尝试这样做
-(void)PostJson {
__block NSMutableDictionary *resultsDictionary;
NSDictionary *userDictionary = [[NSDictionary alloc] initWithObjectsAndKeys:@"first title", @"title",@"1",@"blog_id", nil];//if your json structure is something like {"title":"first title","blog_id":"1"}
if ([NSJSONSerialization isValidJSONObject:userDictionary]) {//validate it
NSError* error;
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:userDictionary options:NSJSONWritingPrettyPrinted error: &error];
NSURL* url = [NSURL URLWithString:@"www.google.com"];
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0];
[request setHTTPMethod:@"POST"];//use POST
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d",[jsonData length]] forHTTPHeaderField:@"Content-length"];
[request setHTTPBody:jsonData];//set data
__block NSError *error1 = [[NSError alloc] init];
//use async way to connect network
[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse* response,NSData* data,NSError* error)
{
if ([data length]>0 && error == nil) {
resultsDictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error1];
NSLog(@"resultsDictionary is %@",resultsDictionary);
} else if ([data length]==0 && error ==nil) {
NSLog(@" download data is null");
} else if( error!=nil) {
NSLog(@" error is %@",error);
}
}];
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12390 次 |
| 最近记录: |