Coder Social home page Coder Social logo

seniverse / seniverse-api-demos Goto Github PK

View Code? Open in Web Editor NEW
231.0 16.0 190.0 179 KB

心知天气 API 产品使用调用示例

Home Page: https://docs.seniverse.com/

HTML 15.17% JavaScript 5.09% PHP 11.20% Python 9.18% Common Lisp 7.37% Java 8.20% Objective-C 18.23% Swift 13.20% Elixir 12.36%
thinkpage api demo jsonp seniverse

seniverse-api-demos's Introduction

心知天气 API 调用示例

All Contributors

请首先查看心知天气的文档来了解心知天气 API 的调用方式,以下是一些不同编程语言的示例代码,也欢迎给此项目提 PR 来添加更多不同语言的调用示例,同时也可以向心知天气申请以获得开发者认证

代码示例

注意:所有代码示例仅做开发参考使用,不要在生产环境下暴露 key!生产环境下请通过后端进行签名验证。

常见问题

  1. 我在本地调试的时候,如何进行域名绑定?

    本地调试时,请在 控制台 -> 产品管理 的对应产品页面将域名绑定填写为:127.0.0.1。若 127.0.0.1 在本机无法访问,请注意是否开启了全局代理。

  2. 为什么我绑定了 127.0.0.1 但访问时报错(没有找到域名,或者报跨域请求的错误,常见于纯前端 API 调用时)?

    请通过 127.0.0.1 的链接,通过 jsonp 的形式访问,而不是 localhostlocalhost 访问会跨域。

    请通过 127.0.0.1 的链接,通过 jsonp 的形式访问,而不是 localhostlocalhost 访问会跨域。

    请通过 127.0.0.1 的链接,通过 jsonp 的形式访问,而不是 localhostlocalhost 访问会跨域。

  3. 关于 API 调用问题

    为了保证您账号的安全,不要纯前端进行 API 调用!仅仅通过前端调用会造成您的 uidkey 暴露,可能会带来不必要的麻烦。推荐的调用方式有:

    • 后端进行 API 调用获取数据后交给前端渲染
    • 或者后端构造 JSONP 形式的请求链接,交给前端调用
  4. 关于 API 调用失败的问题

    可能的原因有:

    • ttl 有效时间过短
    • key 加密后的结果没有通过 Base64 编码
    • 在构造签名的时候,参数的顺序不正确。参与签名的参数有 UNIX 时间戳 ts,签名失效时间 ttl(单位为秒,缺省为 1800,可选)和公钥 uid。请确保:
      1. 参数按照键字典升序排列,例如 ts=1443079775&ttl=300&uid=U123456789 或者 ts=1443079775&uid=U123456789
      2. tsttl 的单位都是秒
    • Last but not least,请将 demo 内的 API key 等参数替换为自己账户的数据

其他第三方相关项目

Contributors

muyinliu
muyinliu

💻
mokeyjay
mokeyjay

💻
Liu Cheng a.k.a MaYi
Liu Cheng a.k.a MaYi

💻
dazheng_wu
dazheng_wu

💻
hewieZ
hewieZ

💻
weishuaigithub
weishuaigithub

💻
Eric SHI
Eric SHI

🚧
ecmadao
ecmadao

🚧
Maples7
Maples7

🚧

seniverse-api-demos's People

Contributors

allcontributors[bot] avatar ecmadao avatar flyingant avatar hewiez avatar longwosion avatar maples7 avatar weishuaigithub avatar wuqingzheng avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

seniverse-api-demos's Issues

Origin 'http://127.0.0.1:8020' is therefore not allowed access.

如您所述,我在我的服务里绑定了域名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.
我查了下 是跨域问题,但是我已经这么做了啊。
另外,代理已关。

访问天气一直报403?

<script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script> <script type="text/javascript" src="./hmac-sha1.js"></script> <script type="text/javascript"> function getWeather(res) { console.log(res); } var UID = "U44633C859"; // 自己的用户ID var KEY = "hfdame1p3sajneur"; // 自己的 Key var API = "http://api.seniverse.com/v3/weather/now.json"; // 获取天气实况 var LOCATION = "beijing"; // 除拼音外,还可以使用 v3 id、汉语等形式 // 获取当前时间戳 var ts = Math.floor((new Date()).getTime() / 1000); // 构造验证参数字符串 var str = "ts=" + ts + "&uid=" + UID; // 使用 HMAC-SHA1 方式,以 API 密钥(key)对上一步生成的参数字符串(raw)进行加密 // 并将加密结果用 base64 编码,并做一个 urlencode,得到签名 sig var sig = CryptoJS.HmacSHA1(str, KEY).toString(CryptoJS.enc.Base64); sig = encodeURIComponent(sig); str = str + "&sig=" + sig; var jsonpCallback = function (data) { var obj = document.getElementById('content'); var weather = data.results[0]; var text = []; text.push("Location: " + weather.location.path); text.push("Weather: " + weather.now.text); text.push("Temperature: " + weather.now.temperature); obj.innerText = text.join("\n"); } // 构造最终请求的 url var url = API + "?location=" + LOCATION + "&" + str + "&callback=getWeather"; // 向 HTML 中动态插入 script 标签,通过 JSONP 的方式进行调用 var newScript = document.createElement('script'); newScript.type = 'text/javascript'; newScript.src = url; $('body').append(newScript); </script>

AP010008

localhost调用,jsonp方式调用返回
{
"status": "Unknown website. Please sign in to http://www.seniverse.com and register your website.",
"status_code": "AP010008"
}
这个本地调试要在绑定域名里增加 127.0.0.1?加了还是不行

-----

var sig = CryptoJS.HmacSHA1(str, KEY).toString(CryptoJS.enc.Base64);没找到方法

Add golang support - 实时天气预报

添加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)
}

error

image
请问我这个是怎么回事

原生js XMLHttp 请求跨域

既然已经绑定了域名为什么还会有跨域问题, 给的案例是用 jq 是做的, 难道我就为了一个天气的 API 还去引入一个包, 鸡肋

android端访问天气api

能够访问接口,但返回{"status":"The API key is invalid.","status_code":"AP010003"},显示API密钥key错误,密匙已经过Base64编码处理,不知是哪错了?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.