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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4760538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 13:57:51  来源:igfitidea点击:

Using Only Javascript To Shrink URLs Using The Bit.ly API

javascriptjqueryapiurl-shortenerbit.ly

提问by Nathan Campos

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?

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

回答by Maksym Kozlenko

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

这是一个如何使用 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);
        }
    );
}

The following code could be used to get a short 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);
});

回答by WebSeed

From the developer best practisespage 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.

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

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

回答by kvista

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

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

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

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

via XmlHttpRequest, for example:

通过 XmlHttpRequest,例如:

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