seniverse / seniverse-api-demos Goto Github PK
View Code? Open in Web Editor NEW心知天气 API 产品使用调用示例
Home Page: https://docs.seniverse.com/
心知天气 API 产品使用调用示例
Home Page: https://docs.seniverse.com/
添加Golang
语言支持, 原本git clone
到本地再git push
失败, 代码就直接放这里了.
欢迎交流, E-Mail: [email protected], GitHub: https://github.com/MulticsYin
/*******************************************************************************************************
访问URL:
https://api.seniverse.com/v3/weather/now.json?key=使用自己的Key&location=zhuhai&language=zh-Hans&unit=c
返回数据:
{'results': [{'last_update': '2017-12-08T09:25:00+08:00',
'location': {'country': 'CN',
'id': 'WEBY8Q5HHUCU',
'name': '珠海',
'path': '珠海,珠海,广东,**',
'timezone': 'Asia/Shanghai',
'timezone_offset': '+08:00'},
'now': {'code': '0', 'temperature': '17', 'text': '晴'}}]}
注:
1. 使用个人开发者帐号返回的数据编写,服务数据不完全,不过看懂了这个程序,其他的都一样啦。
2. 运行程序: go run WeatherNow.go
******************************************************************************************************/
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"reflect"
)
var API = "https://api.seniverse.com/v3/weather/now.json" // API URL,可替换为其他 URL
var KEY = "use Key for youself" // API key
var LOCATION = "zhuhai" // 所查询的位置,可以使用城市拼音、v3 ID、经纬度等
var LANGUAGE = "zh-Hans" // 查询结果的返回语言
var UNIT = "c" // 单位
// URL Access url.
var URL = API + "?" + "key=" + KEY + "&" + "location=" + LOCATION + "&" + "language=" + LANGUAGE + "&" + "unit=" + UNIT
// ResponseJSON Store response data 'results'.
type ResponseJSON struct {
Results []WeatherNow `json:"results,omitempty"`
}
// WeatherNow Basic slice data to 'results'.
type WeatherNow struct {
LastUpdate string `json:"last_update,omitempty"`
Location LocationStruct `json:"location,omitempty"`
Now NowStruct `json:"now,omitempty"`
}
// LocationStruct Store data 'location'.
type LocationStruct struct {
Country string `json:"country,omitempty"`
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Path string `json:"path,omitempty"`
TimeZone string `json:"timezone"`
TimeZoneOffset string `json:"timezone_offset,omitempty"`
}
// NowStruct Store data 'now'.
type NowStruct struct {
Code string `json:"code,omitempty"`
Temperature string `json:"temperature,omitempty"`
Text string `json:"text,omitempty"`
}
func main() {
resp, err := http.Get(URL)
if nil != err {
fmt.Println("HTTP Get error.")
}
defer resp.Body.Close()
ResponseData, err := ioutil.ReadAll(resp.Body)
if nil != err {
fmt.Println("ioutil.ReadAll error.")
}
fmt.Printf("%s\n", ResponseData)
fmt.Println("Type of ResponseData: ", reflect.TypeOf(ResponseData))
var Response ResponseJSON
if err := json.Unmarshal([]byte(ResponseData), &Response); nil != err {
fmt.Println("Unmarshal response weather now data error")
}
fmt.Println(Response.Results[0].LastUpdate)
fmt.Println(Response.Results[0].Location)
fmt.Println(Response.Results[0].Now)
fmt.Println(Response.Results[0].Now.Code)
fmt.Println(Response.Results[0].Now.Temperature)
fmt.Println(Response.Results[0].Now.Text)
}
如您所述,我在我的服务里绑定了域名127.0.0.1,本地跑的项目以127.0.0.1:8020开头,可是
出现如下问题:
XMLHttpRequest cannot load http://api.seniverse.com/v3/weather/now.json?location=%E5%8D%97%E6%B2%99&ts…502083719&uid=uid&sig=sign&callback=foo. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:8020' is therefore not allowed access.
我查了下 是跨域问题,但是我已经这么做了啊。
另外,代理已关。
var sig = CryptoJS.HmacSHA1(str, KEY).toString(CryptoJS.enc.Base64);没找到方法
我觉得公钥私钥太麻烦,有没有办法直接获取token
localhost调用,jsonp方式调用返回
{
"status": "Unknown website. Please sign in to http://www.seniverse.com and register your website.",
"status_code": "AP010008"
}
这个本地调试要在绑定域名里增加 127.0.0.1?加了还是不行
既然已经绑定了域名为什么还会有跨域问题, 给的案例是用 jq 是做的, 难道我就为了一个天气的 API 还去引入一个包, 鸡肋
能够访问接口,但返回{"status":"The API key is invalid.","status_code":"AP010003"},显示API密钥key错误,密匙已经过Base64编码处理,不知是哪错了?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.