bdbergeron / BDBOAuth1Manager

OAuth 1.0a library for AFNetworking 2.x
MIT License
100 stars 35 forks source link

added check for additional token duration key (to make it work with Yahoo OAuth) #2

Closed ishaq closed 10 years ago

ishaq commented 10 years ago

Hello Bergeron,

I am using BDBOAuth1Manager with Yahoo Social APIs, noticed yahoo's token duration key is named different. So I added it.

bdbergeron commented 10 years ago

I would suggest using the Objective-C SDK that Yahoo created instead of a bare-bones OAuth library such as this one, as it appears that their SDKs do a better job at handling tokens and user sessions without requiring a lot of work on the developer's part. Have a look at their documentation at http://developer.yahoo.com/social/sdk/objectivec/.

ishaq commented 10 years ago

yes, I ended up using their ancient SDK. I wanted to use a modern library before but couldn't get their YQL API to work with BDBOAuth1Manager and their web-services are treated as 2nd class citizens.

btw, yahoo's own SDK's token renewal was broken which I fixed (in addition to the crash bug that everyone encounters when they first use it). I guess they started writing it but never finished it (or finished it and never tested it).

bdbergeron commented 10 years ago

After working through Issue #19 and gaining some hands-on experience with Yahoo APIs, I wanted to see if I could get the YQL API working. It turns out to be quite simple - just instantiate an instance of BDBOAuth1Manager with your consumer key and secret and then perform your GET request. The YQL API doesn't require the oauth_token and thus the authorization handshake step is not required.

BDBOAuth1SessionManager *networkManager =
    [[BDBOAuth1SessionManager alloc] initWithBaseURL:[NSURL URLWithString:@"https://query.yahooapis.com/v1/public/"]
                                         consumerKey:@"key"
                                      consumerSecret:@"secret"];

// Add HTTP 400 to acceptable status codes to ensure we recieve API errors (missing parameters, wrong method, etc) as a success callback.
// We'll then need to check the root element in responseObject to see if the API returned an error.
NSMutableIndexSet *statusCodes = networkManager.responseSerializer.acceptableStatusCodes.mutableCopy;
[statusCodes addIndex:400];
networkManager.responseSerializer.acceptableStatusCodes = statusCodes;

[networkManager GET:@"yql"
         parameters:@{@"q": @"SELECT * FROM weather.forecast WHERE woeid = '2354842'", @"format": @"json"}
            success:^(NSURLSessionDataTask *task, id responseObject) {
                // Call succeeded, parse responseObject...
                NSLog(@"%@", responseObject);
            }
            failure:^(NSURLSessionDataTask *task, NSError *error) {
                // Call failed, handle error.
                NSLog(@"%@", error.localizedDescription);
            }];

If the request is successful, responseObject should be an instance of NSDictionary. A printout of it gives the following:

2014-07-10 08:13:33.993 Bossy[18942:60b] {
    query =     {
        count = 1;
        created = "2014-07-10T12:12:20Z";
        lang = "en;q=1";
        results =         {
            channel =             {
                astronomy =                 {
                    sunrise = "6:05 am";
                    sunset = "9:10 pm";
                };
                atmosphere =                 {
                    humidity = 75;
                    pressure = "30.12";
                    rising = 1;
                    visibility = 10;
                };
                description = "Yahoo! Weather for Ann Arbor, MI";
                image =                 {
                    height = 18;
                    link = "http://weather.yahoo.com";
                    title = "Yahoo! Weather";
                    url = "http://l.yimg.com/a/i/brand/purplelogo//uh/us/news-wea.gif";
                    width = 142;
                };
                item =                 {
                    condition =                     {
                        code = 30;
                        date = "Thu, 10 Jul 2014 7:52 am EDT";
                        temp = 63;
                        text = "Partly Cloudy";
                    };
                    description = "\n<img src=\"http://l.yimg.com/a/i/us/we/52/30.gif\"/><br />\n<b>Current Conditions:</b><br />\nPartly Cloudy, 63 F<BR />\n<BR /><b>Forecast:</b><BR />\nThu - Mostly Sunny. High: 75 Low: 56<br />\nFri - Mostly Sunny. High: 78 Low: 61<br />\nSat - Scattered Thunderstorms. High: 79 Low: 66<br />\nSun - Scattered Thunderstorms. High: 81 Low: 62<br />\nMon - Scattered Thunderstorms. High: 78 Low: 57<br />\n<br />\n<a href=\"http://us.rd.yahoo.com/dailynews/rss/weather/Ann_Arbor__MI/*http://weather.yahoo.com/forecast/USMI0028_f.html\">Full Forecast at Yahoo! Weather</a><BR/><BR/>\n(provided by <a href=\"http://www.weather.com\" >The Weather Channel</a>)<br/>\n";
                    forecast =                     (
                                                {
                            code = 34;
                            date = "10 Jul 2014";
                            day = Thu;
                            high = 75;
                            low = 56;
                            text = "Mostly Sunny";
                        },
                                                {
                            code = 34;
                            date = "11 Jul 2014";
                            day = Fri;
                            high = 78;
                            low = 61;
                            text = "Mostly Sunny";
                        },
                                                {
                            code = 38;
                            date = "12 Jul 2014";
                            day = Sat;
                            high = 79;
                            low = 66;
                            text = "Scattered Thunderstorms";
                        },
                                                {
                            code = 38;
                            date = "13 Jul 2014";
                            day = Sun;
                            high = 81;
                            low = 62;
                            text = "Scattered Thunderstorms";
                        },
                                                {
                            code = 38;
                            date = "14 Jul 2014";
                            day = Mon;
                            high = 78;
                            low = 57;
                            text = "Scattered Thunderstorms";
                        }
                    );
                    guid =                     {
                        content = "USMI0028_2014_07_14_7_00_EDT";
                        isPermaLink = false;
                    };
                    lat = "42.29";
                    link = "http://us.rd.yahoo.com/dailynews/rss/weather/Ann_Arbor__MI/*http://weather.yahoo.com/forecast/USMI0028_f.html";
                    long = "-83.72";
                    pubDate = "Thu, 10 Jul 2014 7:52 am EDT";
                    title = "Conditions for Ann Arbor, MI at 7:52 am EDT";
                };
                language = "en-us";
                lastBuildDate = "Thu, 10 Jul 2014 7:52 am EDT";
                link = "http://us.rd.yahoo.com/dailynews/rss/weather/Ann_Arbor__MI/*http://weather.yahoo.com/forecast/USMI0028_f.html";
                location =                 {
                    city = "Ann Arbor";
                    country = "United States";
                    region = MI;
                };
                title = "Yahoo! Weather - Ann Arbor, MI";
                ttl = 60;
                units =                 {
                    distance = mi;
                    pressure = in;
                    speed = mph;
                    temperature = F;
                };
                wind =                 {
                    chill = 63;
                    direction = 360;
                    speed = 6;
                };
            };
        };
    };
}