2013-03-27 4 views
0

J'essaie de partager l'image sur Twitter en utilisant de nouveaux statuts api/update_with_media. Ensuite, j'ai cherché et obtenu ci-dessous le code de la page dev.twitter.com/discussion. Ici, le code est ci-dessous.comment poster l'image à twitter en utilisant http://upload.twitter.com/1/statuses/update_with_media pour ci-dessous ios 5.0

Entrez le code ici

- (NSString *) _uploadImage:(UIImage *)image requestType:(MGTwitterRequestType)requestType responseType:(MGTwitterResponseType)responseType 
{ 

    NSString *boundary = @"----------------------------991990ee82f7"; 

    NSURL *finalURL = [NSURL URLWithString:@"http://upload.twitter.com/1/statuses/update_with_media.json"]; 
    if (!finalURL) 
    { 
     return nil; 
    } 

    NSLog(@"-> Open Connection: %@", finalURL); 


    OAMutableURLRequest *theRequest = [[OAMutableURLRequest alloc] initWithURL:finalURL 
                     consumer:self.consumer 
                     token:_accessToken 
                     realm: nil 
                  signatureProvider:nil]; 

    [theRequest setHTTPMethod:@"POST"]; 
    [theRequest setHTTPShouldHandleCookies:NO]; 

    // Set headers for client information, for tracking purposes at Twitter. 
    [theRequest setValue:_clientName forHTTPHeaderField:@"X-Twitter-Client"]; 
    [theRequest setValue:_clientVersion forHTTPHeaderField:@"X-Twitter-Client-Version"]; 
    [theRequest setValue:_clientURL  forHTTPHeaderField:@"X-Twitter-Client-URL"]; 


    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary]; 
    [theRequest setValue:contentType forHTTPHeaderField:@"content-type"]; 

    NSMutableData *body = [NSMutableData dataWithLength:0]; 
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 

    [body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"media_data[]\"; filename=\"1.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[[NSString stringWithString:[UIImageJPEGRepresentation(image, 1.0) base64EncodingWithLineLength:0]] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"status\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[[NSString stringWithString:@"Honeymoon uploads image\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 

    // -------------------------------------------------------------------------------- 
    // modificaiton from the base clase 
    // our version "prepares" the oauth url request 
    // -------------------------------------------------------------------------------- 
    [theRequest prepare]; 

    [theRequest setHTTPBody:body]; 

    // Create a connection using this request, with the default timeout and caching policy, 
    // and appropriate Twitter request and response types for parsing and error reporting. 
    MGTwitterHTTPURLConnection *connection; 
    connection = [[MGTwitterHTTPURLConnection alloc] initWithRequest:theRequest 
                  delegate:self 
                 requestType:requestType 
                 responseType:responseType]; 

    if (!connection) 
    { 
     return nil; 
    } 
    else 
    { 
     [_connections setObject:connection forKey:[connection identifier]]; 
     //[connection release]; 
    } 

    return [connection identifier]; 
} 

Alors je suis mis en œuvre ce code dans SA_OAuthtwitterengine parce que je suis en utilisant la méthode OAuth pour le texte de poste ou de l'image twitter parce que mon objectif de déploiement d'applications commence forme 3.0 (ios, iPod) , donc j'essaye de cette méthode.

Maintenant, mon problème est de savoir comment créer du code pour envoyer une image à twitter avec un message.

i essayez d'envoyer l'image à twitter en utilisant le type ci-dessous

[_engine sendupdate:@"http://www.prepare-community.com/ShareFiles/index-fr.jpg"]; 

je suis erreur de connexion qui signifie erreur 401.

pouvez-vous s'il vous plaît envoyez-moi comment envoyer une image à twitter en utilisant le code ci-dessus?

+0

http://stackoverflow.com/questions/29555971/ios-unable-to-upload-media-with-twitter-fabric-new-sdk/36509939 # 36509939 – jose920405

Répondre

0

Voici la solution pour le téléchargement d'images en utilisant la nouvelle API Twitter update_with_media. En utilisant cette API, vous pouvez télécharger l'image directement sur le compte Twitter. Le code ci-dessus doit être placé en premier dans le fichier SA_OAuthtwitter engine .m et décalé dans le fichier .h.

Et faites une petite correction qui est où filenamed = 1.jpg placé au lieu de 1.png.

Et maintenant il suffit de mettre le code ci-dessous dans la classe MGTwitterengine.m. ici le code est ci-dessous

- (NSString *)_uploadImage:(UIImage *)image withStatus:(NSString *)status 
{ 
    return [self _uploadImage:image withStatus:status requestType:MGTwitterImageRequest responseType:MGTwitterStatuses]; 
} 

Ensuite, nous utilisons ce code pour télécharger l'image sur Twitter. c'est-à-dire

[_engine _uploadImage: yourpicture withStatus: yourstatus];

Espérons que cela fonctionnera pour vous ....

Questions connexes