Search code examples
actionscript-3debuggingoauth-2.0yahoo-weather-api

Accessing Yahoo Weather API through Flash and Oauth 2.0


I am trying to access weather API through flash.However, stuck on the last step, of getting values from the API in exchange of access token.The problem is that the access token must be passed through authorization header, and Flash has no way to send the authorization header.That is, it doesnot allow it for security reasons.Is their any solution available for this problem ?

Reference : https://developer.yahoo.com/oauth2/guide/apirequests/

It says :

Making API Requests ¶ To make API requests, include the access token in the Authorization header. Note that API requests must be made securely over HTTPS. The header content comprises of the word Bearer followed by the access token. Sample Request Header ¶

GET https://social.yahooapis.com/v1/user/abcdef123/profile?format=json Authorization: Bearer [place your access token here]

Here is the code I am using :

var url = "http://weather.yahooapis.com/forecastrss?w=2444293&u=f"
var access_token = < access token here >
var encoder: Base64Encoder = new Base64Encoder();
encoder.insertNewLines = false;
encoder.encode(access_token);
var urlRequest: URLRequest = new URLRequest("./proxyForYahoo.php");

var urlLoader: URLLoader = new URLLoader();
urlLoader.addEventListener(Event.COMPLETE, onComplete);
urlLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS, onHTTPStatus);
urlLoader.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
urlRequest.method = URLRequestMethod.GET;
urlRequest.contentType = "application/json";

urlRequest.requestHeaders = new Array(
    new URLRequestHeader('Authorization', 'Bearer ' + encoder.toString()),
    new URLRequestHeader('Accept', 'application/json'),
    new URLRequestHeader('Content-Type', 'application/json')

);
var urlVars: URLVariables = new URLVariables();
urlVars.data = url;
urlRequest.data = urlVars;
urlLoader.load(urlRequest);


function onComplete(e: Event): void {
    trace("completed---");
    trace(e.target.data);
}
function onHTTPStatus(e: Event): void {
    trace("onHTTPStatus");
    trace(e.target.data);
}
function onIOError(e: Event): void {
    trace("onIOError");

}
function onSecurityError(e: Event): void {
    trace("onSecurityError");

}

Yahoo proxy :

<?php

$url = $_REQUEST['data'] ;  

$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);

curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_HEADER, 0);

ob_start();

curl_exec ($ch);
curl_close ($ch);
$string = ob_get_contents();

$content = ob_end_clean();

echo $string;

?>

Solution

  • For further reading see : https://developer.yahoo.com/weather/#get-started=

    Below is an example AS3 code for getting Yahoo Weather data without involving Oauth tokens.

    Here you would only need a PHP proxy if your SWF is running from non-HTTPS web location. In such a case, just write a PHP version of that getYahooWeather function where it echo's back the str_YahooResponse to any requesting loaders..

    Test : Simply update str_Woeid = "2444293"; and test compile.

    var str_Woeid : String = "";
    var str_YahooRequest : String = "";
    var str_YahooResponse : String = "";
    
    var Weather_Loader:URLLoader = new URLLoader();
    
    //# get Yahoo response into : str_YahooResponse : via function
    str_Woeid = "2444293"; //# the WOEID of Weather request
    getYahooWeather(str_Woeid, "json"); //# choose : "xml" OR "json" 
    
    function getYahooWeather( inputStr : String, fmt : String = null  ) : void
    {
        if (fmt == "xml") { fmt = "format=xml" }
        else { fmt = "format=json" }
    
        str_YahooRequest = ("https://query.yahooapis.com/v1/public/yql?" 
                            + fmt //either: =json OR =xml
                            + "&q=SELECT%20*%20FROM%20weather.forecast%20WHERE%20u=%27"
                            + "f" //temperature fomat : f = farenheit, c = celsius
                            + "%27%20AND%20woeid%20=%20%27"
                            + inputStr //the WOEID
                            + "%27");
    
        Weather_Loader.addEventListener(Event.COMPLETE, onLoaded);
        Weather_Loader.load(new URLRequest(str_YahooRequest));
    }
    
    function onLoaded(evt:Event) : void 
    {
        //# put weather data to string (then parse as JSON or XML)
        str_YahooResponse = evt.target.data;
        trace("Yahoo Weather Data ::::::::: " + str_YahooResponse);
    }
    

    Now you can use the updated string str_YahooResponse with relevant parser...
    for example as : myJSON.parse(str_YahooResponse);.