Lately I’ve been playing with various website api calls.
I recently switched to looking at Etsy for my next project.
Etsy uses oauth which I am not very familiar with…
I must first generate a set of temporary credentials to produce a secure login to authenticate against a user…
Trying to recreate the following php example from etsy’s api documentation::
$oauth = new OAuth(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET);
// make an API request for your temporary credentials
$req_token = $oauth->getRequestToken("https://openapi.etsy.com/v2/oauth/request_token?scope=email_r%20listings_r", 'oob');
I have translated it over to asobjc as follows
set testURL to "https://openapi.etsy.com/v2/oauth/request_token?&scope=email_r%20listings_r&oauth_consumer_key=etsyConsumerKey&oauth_consumer_secret=etsyConsumerSecret"
set theHttp to ((current application's NSMutableURLRequest's alloc)'s initWithURL:(current application's NSURL's URLWithString:testURL))
theHttp's setHTTPMethod:"POST"
set {theData, theResponse, theError} to current application's NSURLConnection's sendSynchronousRequest:theHttp returningResponse:(reference) |error|:(reference)
log theResponse
I get a response however they are always filled with errors.
“X-Error-Detail” = “oauth_problem=parameter_absent&oauth_parameters_absent=oauth_signature%26oauth_signature_method%26oauth_nonce%26oauth_timestamp”;
Basically a bunch of errors saying I am missing parameters… how does one initiate such a oauth call in asobjc?
I am still new to the whole translation thing and I have no previous knowledge of objective-c… so I’m kinda struggling with the more in depth translations.
Now i ran into the problem that all the oauthconsumer files are throwing all sorts of Semantic Issues (retain) and ARC Restrictions as I am figuring the OAuthConsumer is an older outdated library set…?
I have figured out that if I include some compiler flags for those Arc issues I am able to compile.
I used “-fno-objc-arc” on all .m files that were throwing those errors.
Now I’m back to having trouble with the translating of
Now I’m noticing the colons after the “didFinishWithData” and “didFailWithError”… I have no idea how to translate this over
not sure if it helps, but this is the .h file @interface OADataFetcher : NSObject { @private
OAMutableURLRequest *request;
NSURLResponse *response;
NSURLConnection *connection;
NSMutableData *responseData;
id delegate;
SEL didFinishSelector;
SEL didFailSelector;
}
Normally you just supply a string for selectors in ASObjC. As in performSelector:“doSomething:” … That should work, as long as the framework includes a suitable .bridgesupport file.
StefanK, not translating the whole library… just trying to follow an OAuthConsumer “walk through” and translate that over to work with the OAuthConsumer Library
here → http://code.google.com/p/oauthconsumer/wiki/UsingOAuthConsumer
Following that I have translated it into the following
on etsyApiTempToken:sender
set app_id to "XXXX"
set app_secret to "XXXX"
set urlString to "https://openapi.etsy.com/v2/oauth/request_token?"
set theConsumer to current application's OAConsumer's alloc's initWithKey:app_id secret:app_secret
set theUrl to current application's NSURL's URLWithString:urlString
set theRequest to current application's OAMutableURLRequest's alloc's initWithURL:theUrl consumer:theConsumer token:(reference) realm:(reference) signatureProvider:(reference)
theRequest's setHTTPMethod:"POST"
set theFetcher to current application's OADataFetcher's alloc's init
theFetcher's fetchDataWithRequest:theRequest delegate:me didFinishSelector:"requestTokenTicket:didFinishWithData" didFailSelector:"requestTokenTicket:didFailWithError"
end etsyApiTempToken:
on requestTokenTicket:(ticket) didFinishWithData:(theData)
if ticket's didSucceed = true then
set responseBody to current application's NSString's alloc's initWithData:theData encoding:NSUTF8StringEncoding
set requestToken to current application's OAToken's alloc's initWithHTTPResponseBody:responseBody
end if
end requestTokenTicket:didFinishWithData:
I am still having trouble with
theFetcher’s fetchDataWithRequest:theRequest delegate:me didFinishSelector:“requestTokenTicket:didFinishWithData” didFailSelector:“requestTokenTicket:didFailWithError”
I don’t know the library in question, but you can’t have a selector called “requestTokenTicket:didFinishWithData”. It would have to be “requestTokenTicket:didFinishWithData:”.
ITS ALIVE!
Thank you guys for all the help in guiding me towards a final working OAuth!
Hope this can help someone else in the future.
on etsyRequestToken:sender
set app_id to "XXXX"
set app_secret to "XXXX"
set urlString to "https://openapi.etsy.com/v2/oauth/request_token?"
set theConsumer to current application's OAConsumer's alloc's initWithKey:app_id secret:app_secret
set theUrl to current application's NSURL's URLWithString:urlString
set theRequest to current application's OAMutableURLRequest's alloc's initWithURL:theUrl consumer:theConsumer token:(missing value) realm:(missing value) signatureProvider:(missing value)
theRequest's setHTTPMethod:"GET"
set theFetcher to current application's OADataFetcher's alloc's init()
theFetcher's fetchDataWithRequest:theRequest delegate:self() didFinishSelector:"requestTokenTicket:didFinishWithData:" didFailSelector:"requestTokenTicket:didFailWithError:"
end etsyRequestToken:
on requestTokenTicket:(ticket) didFinishWithData:(theData)
set theResponse to current application's NSString's alloc()'s initWithData:theData encoding:(current application's NSUTF8StringEncoding)
log theResonse
end requestTokenTicket:didFinishWithData: