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;
?>