twitpic API GSTwitPicEngine error while setAccessToken

2.2k Views Asked by At

I tried to use GSTwitPicEngine class https://github.com/Gurpartap/GSTwitPicEngine for sharing image on twitter
but its giving error

As we have to set [twitpicEngine setAccessToken:token];
if I am right, I am using _accessToken of class SA_OAuthTwitterEngine as token

_accessToken was private in class SA_OAuthTwitterEngine, I set it to public and also

@property (nonatomic, readonly) OAToken *accessToken;
@synthesize accessToken = _accessToken;

and then in action

twitPicEngine = [GSTwitPicEngine twitpicEngineWithDelegate:self];
[twitPicEngine setAccessToken:twitterObj.accessToken];
[twitPicEngine uploadPicture:[shareDict objectForKey:@"image"] withMessage:[shareDict objectForKey:@"image_msg"]];

But Application getting crash while setAccessToken with log

-[OAToken parameters]: unrecognized selector sent to instance 0x6327e30
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[OAToken parameters]: unrecognized selector sent to instance 0x6327e30'

Please help if you able to find any thing wrong

Amit Battan

2

There are 2 best solutions below

0
On

You can set Access token & Access Secret for twitpic after getting from twitter engine i.e

SA_OAuthTwitterEngine.m

  • (void) setAccessToken: (OAServiceTicket *) ticket withData: (NSData *) data

Create object of "GSTwitPicEngine.m" class in "SA_OAuthTwitterEngine.m" & pass Access token & Access Secret to "GSTwitPicEngine.m" and set delegate methods of "GSTwitPicEngine.m" in "SA_OAuthTwitterEngine.m"

And one more change...

must cross check URLEncoding while genrating OAuth Header i.e

[requestHeader generateRequestHeaders]

this will be according to "OAuthConsumer" classes in Twitter Library

5
On

Unfortunately, GSTwitPicEngine is not using the same version of oauth-consumer than the fabulous Twitter+Oauth library (SAOAuthTwitterEngine). I assume you are using that library for posting messages to twitter.

The crash is because OAToken from Twitter+Oauth doesn´t implement the "parameters" method.

Today I spent the entire morning tweaking the several libraries to avoid crashes. Here you can download a sample project I created for posting a twitpic photo to twitter with a message.

TestTwitpic

The project has all the latest versions of all libraries from github.

Instructions for making my TestTwitpic project work:

In TestTwitpic-Prefix.pch set the variables for:

#define kTwitterOAuthConsumerKey @""
#define kTwitterOAuthConsumerSecret @""
#define kTwitPicAPIKey @""

In RootViewController you can change these lines to your needs. To change the photo:

//change [UIImage imageNamed:@"image.jpg"] for whatever UIImage you want to upload
//change @"my photo" for whatever title you want for your photo in twitpic website
[twitpicEngine uploadPicture:[UIImage imageNamed:@"image.jpg"] withMessage:@"my photo"];

and this one to change message sent to twitter:

//I post to twitter the title of the photo and the twitpic url but you can post whatever you want
[engine sendUpdate:[NSString stringWithFormat:@"%@ %@", [[[response objectForKey:@"request"] userInfo] objectForKey:@"message"], [[response objectForKey:@"parsedResponse"] objectForKey:@"url"]]];

If you want to create your own project based on this sample. Do the following:

  1. Import into your project (drag and drop) the Twitpic folder with all the libraries that are inside.
  2. Add these frameworks to your project:
    • CoreGraphics
    • libz.1.2.3
    • libxml2
    • MobileCoreServices
    • SystemConfiguration
    • CFNetwork
    • Security
  3. In Build Settings add "$SDKROOT/usr/include/libxml2" to the "Header Search Paths" (mark it as recursive)
  4. In Build Settings add -lxml2 to "Other linker flags"

If you want to know what I did for fixing the libraries, I will tell you more or less what I remember I did:

  1. Import Twitter+Oauth, GSTwitPicEngine, OARequestHeader, TouchJSON, and ASIHTTPRequest libraries.
  2. Set GSTwitPicEngine to use TouchJSON instead of YAJL.
  3. Added the category NSString+URLEncoding from oauth-consumer github project as OARequestHeader was missing it.
  4. Modified all occurrences of NSDictionary* foo = [toke parameters] inside OARequestHeader with the line:
    • NSDictionary *foo = [NSDictionary dictionaryWithObject:[token key] forKey:@"oauth_token"];
  5. Created a method getAccessToken in SAOAuthTwitterEngine to return the private _accessToken variable.
  6. In requestFinished: inside GSTwitPicEngine
    • change the line:
      • response = [[CJSONDeserializer deserializer] deserialize:responseString error:&error];
    • with the line:
      • response = [[CJSONDeserializer deserializer] deserialize:[responseString dataUsingEncoding:NSUTF8StringEncoding] error:&error];
    • as GSTwitPicEngine wrongly assumed deserialize:error: method accepts an NSString instead of an NSData.