通过 Flash 和 Oauth 2.0 访问 Yahoo Weather API

Accessing Yahoo Weather API through Flash and Oauth 2.0

我正在尝试通过 flash.However 访问天气 API,卡在最后一步,从 API 获取值以交换访问 token.The 问题是访问令牌必须通过授权 header 传递,而 Flash 无法发送授权 header.That 是,出于安全考虑,它不允许这样做 reasons.Is 他们是否有解决此问题的方法?

参考: https://developer.yahoo.com/oauth2/guide/apirequests/

它说:

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]

这是我使用的代码:

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");

}

雅虎代理:

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

?>

如需进一步阅读,请参阅:https://developer.yahoo.com/weather/#get-started=

下面是在不涉及 Oauth 令牌的情况下获取 Yahoo Weather 数据的示例 AS3 代码。

如果您的 SWF 是来自非 HTTPS 网站的 运行,您只需要一个 PHP 代理。在这种情况下,只需编写该 getYahooWeather 函数的 PHP 版本,其中 echostr_YahooResponse 返回给任何请求加载程序。

Test : 只需更新 str_Woeid = "2444293"; 并测试编译。

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);
}

现在您可以将更新后的字符串 str_YahooResponse 与相关解析器一起使用...
例如:myJSON.parse(str_YahooResponse);.