objective c - How to change profile picture using TWRequest -


how change profile picture using twrequest , post timeline using code

-(ibaction)posttotwitter { acaccountstore *account = [[acaccountstore alloc] init]; acaccounttype *accounttype = [account accounttypewithaccounttypeidentifier:acaccounttypeidentifiertwitter];

// request access user access twitter account [account requestaccesstoaccountswithtype:accounttype withcompletionhandler:^(bool granted, nserror *error)   {      // did user allow access?      if (granted == yes)      {          // populate array available twitter accounts          nsarray *arrayofaccounts = [account accountswithaccounttype:accounttype];           // sanity check          if ([arrayofaccounts count] > 0)           {              // keep simple, use first account available              acaccount *acct = [arrayofaccounts objectatindex:0];               // build twitter request              //http://api.twitter.com/1/account/update_profile_image.json                //https://upload.twitter.com/1/statuses/update_with_media.json               twrequest *postrequest = [[twrequest alloc] initwithurl:[nsurl urlwithstring:@"https://upload.twitter.com/1/statuses/update_with_media.json"] parameters:nil requestmethod:twrequestmethodpost];               //nsdata *mydata = uiimagepngrepresentation(self.selectedimage);             nsdata *mydata =  uiimagejpegrepresentation(self.selectedimage, 0.5);               [postrequest addmultipartdata:mydata withname:@"media" type:@"image/jpg"];               [postrequest setaccount:acct];               // block handler manage response              /*                     [postrequest addmultipartdata:[base64 datausingencoding:nsutf8stringencoding] // base64 nsstring of encoded image                withname:@"image"                type:@"multipart/form-data"];                // post request*/               [postrequest performrequestwithhandler:^(nsdata *responsedata, nshttpurlresponse *urlresponse, nserror *error)                {                   nslog(@"twitter response, http response: %i", [urlresponse statuscode]);               }               ];        }     }  }]; 

}

i struggled too, using apple's wwdc11 slides guide, seems twitter has changed api since (or slide had bug).

the solution simple: need use multipart data name of "image" instead of "media". , think type should "image/jpeg", not "image/jpg".

so change line follows:

[postrequest addmultipartdata:mydata withname:@"image" type:@"image/jpeg"]; 

Comments

Popular posts from this blog

jquery - Invalid Assignment Left-Hand Side -

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -