SharePoint天气Web部件

时间:2019-03-25 16:05:53

标签: sharepoint-2013 web-parts

有人可以帮助我在我的sharepoint 2013网站上找到免费天气预报吗?

我发现了一些有限调用的API,但并非完全免费。

1 个答案:

答案 0 :(得分:0)

我通过Yahoo Weather API做了简单的测试。

在Apps中创建一个应用,以便您可以使用id&key&secret进行请求。

enter image description here

<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/hmac-sha1.js">
    </script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/components/enc-base64.js">
    </script>

    <script type="text/javascript">
        var url = 'https://weather-ydn-yql.media.yahoo.com/forecastrss';
        var method = 'GET';
        var app_id = '80ggw87i';
        var consumer_key = 'yourkey';
        var consumer_secret = 'yoursecret';
        var concat = '&';
        var query = { 'location': 'sunnyvale,ca', 'format': 'json' };
        var oauth = {
            'oauth_consumer_key': consumer_key,
            'oauth_nonce': Math.random().toString(36).substring(2),
            'oauth_signature_method': 'HMAC-SHA1',
            'oauth_timestamp': parseInt(new Date().getTime() / 1000).toString(),
            'oauth_version': '1.0'
        };

        var merged = {};
        $.extend(merged, query, oauth);
        // Note the sorting here is required
        var merged_arr = Object.keys(merged).sort().map(function (k) {
            return [k + '=' + encodeURIComponent(merged[k])];
        });
        var signature_base_str = method
          + concat + encodeURIComponent(url)
          + concat + encodeURIComponent(merged_arr.join(concat));

        var composite_key = encodeURIComponent(consumer_secret) + concat;
        var hash = CryptoJS.HmacSHA1(signature_base_str, composite_key);
        var signature = hash.toString(CryptoJS.enc.Base64);

        oauth['oauth_signature'] = signature;
        var auth_header = 'OAuth ' + Object.keys(oauth).map(function (k) {
            return [k + '="' + oauth[k] + '"'];
        }).join(',');

        $.ajax({
            url: url + '?' + $.param(query),
            headers: {
                'Authorization': auth_header,
                'X-Yahoo-App-Id': app_id
            },
            method: 'GET',
            success: function (data) {
                console.log(data);
                debugger;
            }
        });
    </script>

您需要使用适当的CSS绑定数据。

enter image description here