【问题标题】:How to post an image in a twitter account, using oauth in iOS?如何在 Twitter 帐户中发布图像,在 iOS 中使用 oauth?
【发布时间】:2013-12-18 03:26:53
【问题描述】:

我已经成功接收到如下的 twitter oauth 数据(在用户登录后),尽管我搜索了很多,但我还没有找到一种可以很好地发布图片的方法在用户的 Twitter 个人资料中。有详细教程吗?

  oauth_token=xxxxxxxxjU8gs6&oauth_token_secret=xxxxD7ITa&user_id=xxx73&screen_name=manostijeras&oauth_verifier=xx07

谢谢

PS:我使用本教程接收 twitter oauth 数据 http://www.icodeblog.com/2010/09/16/dealing-with-the-twitter-oauth-apocalypse/

【问题讨论】:

    标签: ios image post twitter twitter-oauth


    【解决方案1】:

    这是程序->

    OAToken *token = [[OAToken alloc] initWithKey:OauthAccessToken secret:OauthAccessSecrateKey]; //Set user Oauth access token and secrate key
    OAConsumer *consumer = [[OAConsumer alloc] initWithKey:ConsumerToken secret:ConsumerSecrateKey]; // Application cosumer token and secrate key
    
    // Url for upload pictures
    NSURL *finalURL = [NSURL URLWithString:@"https://api.twitter.com/1.1/statuses/update_with_media.json];
    
    OAMutableURLRequest *theRequest = [[OAMutableURLRequest alloc] initWithURL:finalURL
                                                                       consumer:consumer
                                                                          token:token
                                                                          realm: nil
                                                              signatureProvider:nil];
    
    [theRequest setHTTPMethod:@"POST"];
    [theRequest setTimeoutInterval:120];
    [theRequest setHTTPShouldHandleCookies:NO];
    
    // Set headers for client information, for tracking purposes at Twitter.(This is optional)
    [theRequest setValue:@"TestIphone" forHTTPHeaderField:@"X-Twitter-Client"];
    [theRequest setValue:@"1.0" forHTTPHeaderField:@"X-Twitter-Client-Version"];
    [theRequest setValue:@"http://www.TestIphone.com/" forHTTPHeaderField:@"X-Twitter-Client-URL"];
    
    NSString *boundary = @"--Hi all, First Share"; // example taken and implemented.
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
    [theRequest setValue:contentType forHTTPHeaderField:@"content-type"];
    
    NSMutableData *body = [NSMutableData data];
    
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"status\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"%@",@"latest upload"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"media[]\"; filename=\"vizllx_pic1.png\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:UIImageJPEGRepresentation([UIImage imageNamed:@"vizllx_pic.png"], 0.5)]];
    [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    
    [theRequest prepare];
    
    NSString *oAuthHeader = [theRequest valueForHTTPHeaderField:@"Authorization"];
    [theRequest setHTTPBody:body];
    
    NSHTTPURLResponse *response = nil;
    NSError *error = nil;
    
    NSData *responseData = [NSURLConnection sendSynchronousRequest:theRequest
                                                 returningResponse:&response                            
                                                             error:&error];
    NSString *responseString = [[NSString alloc] initWithData:responseData                                
                                                     encoding:NSUTF8StringEncoding];
    

    【讨论】:

    • 感谢您的回答,但这不是我要找的。我正在构建的应用程序无法使用存储在设置中的 Twitter 帐户。相反,系统会提示用户登录并授权该应用程序。然后我收到 oauth 数据,之后我想将图像发布到用户的个人资料中
    • 谢谢。我现在去看看
    • 非常感谢。它工作得很好。你拯救了我的一天!
    • @user2879002- 接受答案 mate ,以便对其他用户有所帮助。
    • 有谁知道,是否可以发布视频而不是图片?正如我在一些答案中看到的那样,你不能。只能通过twitvid。这是真的吗?谢谢
    猜你喜欢
    • 2012-10-06
    • 1970-01-01
    • 2016-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-18
    • 2019-03-06
    相关资源
    最近更新 更多