【发布时间】:2014-07-02 10:18:20
【问题描述】:
有什么简单的方法可以验证身份并将推文发布到 twitter?
我已经找到了post方法:
- (void)postImage:(UIImage *)image withStatus:(NSString *)status
{
ACAccountType *twitterType = [self.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
SLRequestHandler requestHandler = ^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
if (responseData) {
NSInteger statusCode = urlResponse.statusCode;
if (statusCode >= 200 && statusCode < 300) {
NSDictionary *postResponseData =
[NSJSONSerialization JSONObjectWithData:responseData
options:NSJSONReadingMutableContainers
error:NULL];
NSLog(@"[SUCCESS!] Created Tweet with ID: %@", postResponseData[@"id_str"]);
}
else {
NSLog(@"[ERROR] Server responded: status code %d %@", statusCode,
[NSHTTPURLResponse localizedStringForStatusCode:statusCode]);
}
}
else {
NSLog(@"[ERROR] An error occurred while posting: %@", [error localizedDescription]);
}
};
ACAccountStoreRequestAccessCompletionHandler accountStoreHandler =
^(BOOL granted, NSError *error) {
if (granted) {
NSArray *accounts = [self.accountStore accountsWithAccountType:twitterType];
NSURL *url = [NSURL URLWithString:@"https://api.twitter.com"
@"/1.1/statuses/update_with_media.json"];
// NSDictionary *params = @{@"status" : status};
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
status, @"status",
nil];
SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeTwitter
requestMethod:SLRequestMethodPOST
URL:url
parameters:params];
NSData *imageData = UIImageJPEGRepresentation(image, 1.f);
[request addMultipartData:imageData
withName:@"media[]"
type:@"image/jpeg"
filename:@"image.jpg"];
[request setAccount:[accounts lastObject]];
[request performRequestWithHandler:requestHandler];
}
else {
NSLog(@"[ERROR] An error occurred while asking for user authorization: %@",
[error localizedDescription]);
}
};
[self.accountStore requestAccessToAccountsWithType:twitterType
options:NULL
completion:accountStoreHandler];
}
但是我遇到了一个错误
:[ERROR] 服务器响应:状态码 401 未授权
如何实现认证??
我已经在 twitter 上注册了我的应用,并且获得了两个 api 密钥(普通密钥和秘密密钥)。
【问题讨论】: