自 1 月 3 日起,雅虎天气有了一个需要 oAuth 的新天气 API https://developer.yahoo.com/weather/
现在我已经从 yahoo 获得了我的客户端 ID(消费者 key )和我的客户端 key (消费者 key )。我的问题是如何在 jquery 或 javascript 中使用 oAuth 调用此 API?
这是调用的样子:
GET /forecastrss?location=sunnyvale,ca HTTP/1.1
Host: weather-ydn-yql.media.yahoo.com
Yahoo-App-Id: YOUR_APP_ID
Authorization: OAuth
oauth_consumer_key="YOUR_CONSUMER_KEY",oauth_signature_method="HMAC-SHA1",oauth_timestamp="YOUR_TIMESTAMP",oauth_nonce="YOUR_NONCE",oauth_version="1.0",oauth_signature="YOUR_GENERATED_SIGNATURE"
cache-control: no-cache
它声明“还请在 header 中包含您的 OAuth 应用程序 ID。”
这在 jquery 中会是什么样子?
我尝试了以下方法:
$.get("https://weather-ydn-yql.media.yahoo.com/forecastrss", { location: "sunnyvale,ca", format: "json", oauth_consumer_key: "Client ID (Consumer Key)", oauth_signature_method: "HMAC-SHA1" } ).done(function( data ) {
console.log("Data Loaded: " + data);
});
我得到这个错误:
ERR_ABORTED 401 (Unauthorized)
最佳答案
虽然我没有专门运行这段代码,但它应该像 jQuery 中的其他所有 get 请求一样工作
$.get( url, { location: "sunnyvale,ca", format: "json", oauth_consumer_key: oauth_consumer_key, oauth_signature_method: oauth_signature_method, ... } )
.done(function( data ) {
alert( "Data Loaded: " + data );
});
引用文献 1:jQuery API https://api.jquery.com/jQuery.get/
Ref2:雅虎 API:https://developer.yahoo.com/weather/documentation.html
关于javascript - jQuery 中的 Yahoo New Weather API oAuth 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54112880/