2014/11/11

[iOS][Objective-C][SinaWeibo]SocialFrameworkで画像をポスト

昨日は、Twitterに画像をポストする方法を書いたが今回はsina weiboに画像を投稿する方法について。

2/statuses/uploadを読むと、

https://upload.api.weibo.com/2/statuses/upload.json
にPOSTしろと書かれているので、まずは、下のような形でACAccountを取得する
ACAccount *aCAccount;
ACAccountStore *aCAccountStore = [[ACAccountStore alloc] init];
ACAccountType *aCAccountType = [aCAccountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierSinaWeibo];
[aCAccountStore requestAccessToAccountsWithType:aCAccountType options:nil completion:^(BOOL granted,NSError *error){
    //許可がおりた時
    if (granted) {
        dispatch_async(dispatch_get_main_queue(), ^{
            NSArray *acAcounts = [aCAccountStore accountsWithAccountType:aCAccountType];
            NSUInteger count = [acAcounts count];
            //今回は、1個のアカウントの場合を考える
            aCAccount = (ACAccount *)[acAcounts objectAtIndex:0];
        });
    }
}];
次に下のように組んでPOSTを行った。
//POSTするimage
UIImage *image;
SLRequest *sLRequest = [SLRequest requestForServiceType:SLServiceTypeSinaWeibo
                                                  requestMethod:SLRequestMethodPOST
                                                            URL:[NSURL URLWithString:@"https://upload.api.weibo.com/2/statuses/upload.json"]
                                                     parameters:@{@"status":@"test"}];
[sLRequest addMultipartData:UIImageJPEGRepresentation(image,1.0)
                           withName:@"pic"
                               type:@"image/jpeg"
                           filename:@"image.jpg"];
sLRequest.account = aCAccount;
[sLRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error){
    if (!error) {
       NSDictionary *dictJSON = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:nil];
       dispatch_async(dispatch_get_main_queue(), ^{

       });
    }
}];
実行した結果、無事に画像付メッセージをPOSTすることができた。

0 コメント:

コメントを投稿