使用 Bit.ly API 仅使用 Javascript 来缩小 URL

发布于 2024-10-13 09:33:34 字数 169 浏览 2 评论 0原文

这些天我在玩一点 Javascript...我使用 bit.ly 缩小了一些 URL 来发布它们,然后我开始考虑一个自动化过程,可以使用他们的 API 来缩小我想要的 URL,然后我查看了查看他们的文档,我看到他们只支持 PHP(带有一些 Javascript),但无论如何我可以只使用 Javascript 来实现这一点?

I'm playing a bit with Javascript these days... I was shrinking some URLs using bit.ly to tweet them, then I started to think on a automated process that could use their API to shrink the URLs I wanted, then I looked up on their documentation, and I saw that they only support PHP(with some Javascript), but there is anyway that I could make this using only Javascript?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

后来的我们 2024-10-20 09:33:34

以下是如何使用 Bitly API 和 jQuery 获取缩短的 URL 的示例,无需服务器端代码。

function get_short_url(long_url, login, api_key, func)
{
    $.getJSON(
        "http://api.bitly.com/v3/shorten?callback=?", 
        { 
            "format": "json",
            "apiKey": api_key,
            "login": login,
            "longUrl": long_url
        },
        function(response)
        {
            func(response.data.url);
        }
    );
}

以下代码可用于获取短 URL:

/*
Sign up for Bitly account at
 https://bitly.com/a/sign_up

and upon completion visit
https://bitly.com/a/your_api_key/ 
to get "login" and "api_key" values
*/
var login = "LOGIN_HERE";
var api_key = "API_KEY_HERE";
var long_url = "http://www.kozlenko.info";

get_short_url(long_url, login, api_key, function(short_url) {
    console.log(short_url);
});

Here is an example how to get a shortened URL with Bitly API and jQuery, no server side code required.

function get_short_url(long_url, login, api_key, func)
{
    $.getJSON(
        "http://api.bitly.com/v3/shorten?callback=?", 
        { 
            "format": "json",
            "apiKey": api_key,
            "login": login,
            "longUrl": long_url
        },
        function(response)
        {
            func(response.data.url);
        }
    );
}

The following code could be used to get a short URL:

/*
Sign up for Bitly account at
 https://bitly.com/a/sign_up

and upon completion visit
https://bitly.com/a/your_api_key/ 
to get "login" and "api_key" values
*/
var login = "LOGIN_HERE";
var api_key = "API_KEY_HERE";
var long_url = "http://www.kozlenko.info";

get_short_url(long_url, login, api_key, function(short_url) {
    console.log(short_url);
});
無心 2024-10-20 09:33:34

根据 JavaScript 的执行位置,您始终可以使用 bit.ly REST API:

通过 XmlHttpRequest http://code.google.com/p/bitly-api/wiki/ApiDocumentation

,例如:

http://api.bit.ly/v3/shorten?login=bitlyapidemo&apiKey=R_0da49e0a9118ff35f52f629d2d71bf07&longUrl=http%3A%2F%2Fbetaworks.com%2F&format=json

Depending on where the JavaScript is executing, you could always use the bit.ly REST API:

http://code.google.com/p/bitly-api/wiki/ApiDocumentation

via XmlHttpRequest, for example:

http://api.bit.ly/v3/shorten?login=bitlyapidemo&apiKey=R_0da49e0a9118ff35f52f629d2d71bf07&longUrl=http%3A%2F%2Fbetaworks.com%2F&format=json
柠北森屋 2024-10-20 09:33:34

来自 bitly 上的开发人员最佳实践页面:

为了确保您的 API 密钥和/或 OAuth 访问令牌的安全,我们强烈建议您尽可能向 bitly API 服务器端发出请求。

通过客户端 Javascript 向 bitly API 发出的任何请求都存在您的 OAuth 令牌或 API 密钥被泄露的风险,但您可以采取一些步骤来部分缓解此风险。最重要的是,切勿在页面中内嵌您的 api_key 或 access_token。将对 api_key 或 access_token 的任何引用保留在页面中包含的外部 javascript 文件中包含的代码中。为了提高安全性,不要将密钥或令牌本身包含在 javascript 代码中的任何位置,而是进行 ajax 调用来加载它,并将其保存在存储在私有范围方法中的变量中。有关此实现的示例,请参阅我们的示例 html 和包含的 javascript 文件。

From the developer best practises page on bitly:

To ensure the security of your API key and/or OAuth access token, we strongly suggest that you make requests to the bitly API server-side whenever possible.

Any requests to the bitly API made via client-side Javascript present the risk of your OAuth token or API key being compromised, but there are steps you can take to partially mitigate this risk. Most importantly, never include your api_key or access_token inline in the page. Keep any references to your api_key or access_token in code that is contained in external javascript files which are included in the page. For additional security, don't have the key or token itself contained anywhere in your javascript code, but rather make an ajax call to load it, and keep it in a variable stored in a privately scoped method. For an example of this implementation, please see our sample html and included javascript files.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文