集成到 Bit.ly API 版本 4 - 如何生成 OAuth2 令牌以与 Bit.ly API V4 集成并创建缩短的 url?

And*_*ese 7 api rest bit.ly postman

我在尝试生成OAuth2 令牌以与Bitly API v4集成时遇到了最困难的时间。他们的文档很难理解,我在网上或 StackOverflow 上找不到太多关于如何完成它的信息。我能够让它工作,并希望与将来会挠头的其他人分享解决方案。

以下是生成通用访问令牌、检索组 Guid生成 OAuth2 令牌和调用 Bitly缩短API 请求的分步指南。

And*_*ese 14

Bitly API -获取oAuth2 访问令牌和使用缩短请求以使用Postman检索 Tiny Url 的步骤:

在花了半天的时间试图弄清楚这一点之后,我想我会发布这个,以便它可以帮助尝试与Bitly API V4集成的人。

注意: Bitly API V3 将于 2020 年 3 月 1 日停用,因此请务必尽快迁移到 V4。为了保护隐私,响应中的所有 guid 和其他私有字段都已更改。

单击此链接可参考Bitly API 文档

1.)生成访问令牌:

使用您的凭据登录 bitly.com,然后单击右上角的汉堡菜单。单击您的用户名,然后单击通用访问令牌。需要此通用访问令牌来检索您的组 Guid。您将需要此 Group Guid 来制作缩短 URL 请求以及您的访问令牌。

2.)检索组指南:

https://api-ssl.bitly.com/v4/groups发出GET请求以获取您的 group_guid。您将在授权字段中使用您的通用访问令牌

下面是标题的外观图片:

组指南标题

你应该得到一个类似于下面的 JSON 的 JSON 响应:

Retrieve Group Guid GET 响应的 JSON 响应:

{
    "groups": [
        {
            "created": "2019-12-18T13:45:21+0000",
            "modified": "2019-12-18T13:45:21+0000",
            "bsds": [],
            "guid": "XZXUYSJAJ43", 
            "organization_guid": "JsjFtE841883",
            "name": "username",
            "is_active": true,
            "role": "org-admin",
            "references": {
                "organization": "https://api-ssl.bitly.com/v4/organizations/JsjFtE841883"
            }
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

您的guid值是您在缩短和其他请求中使用的group_guid

3.)检索您的 client_id 和 client_secret

通过使用您的用户凭据登录 bitly.com 来注册您的应用程序,您的应用程序将被分配一个 client_id 和一个 client_secret。您可以通过点击页面右侧的汉堡包菜单上做到这一点,点击您的用户名,注册OAuth的应用程序,并在底部的应该是一个按钮,上面写着注册新的APP

4.)生成 OAuth2 访问令牌

Refer to the Bitly Documentation and under the Authentication tab navigate to "Exchanging a Username and Password for an Access Token".

• Open Postman

• Create a new POST request to https://api-ssl.bitly.com/oauth/access_token.

• Copy the Curl Command but replace username with Bitly Login Username and password with your Bitly Login Password.

• Click on the Import tab and select Past Raw Text and paste over curl command

• In the Authorization tab paste your client_id into the Username field and client_secret into the Password field. This will do the base64encoding for you.

• Your Headers only need the Host and Content-Type key and Value. Your Authorization will be be a temporary header field and will be automatically generated for you.

• Your Body needs:

grant_type: password (the actual word password)

username: your username for bitly.com

password: your password for for bitly.com

Below is a picture of what your Body should look like:

在此处输入图片说明

• Click Send and you will receive your access token.

5.) Shorten URL

• Open new tab in Postman and make a Post request to https://api-ssl.bitly.com/v4/shorten

Headers should include:

主机api-ssl.bitly.com

授权Bearer(第 4 步中的访问令牌)

内容类型应用程序/json

• 您的正文应包含此 JSON:

{
  "long_url": "http://twitter.com",
  "group_guid": "guid field from Group Guid Response in Step 2"
}
Run Code Online (Sandbox Code Playgroud)

• 单击发送,您应该会看到如下所示的 JSON 响应:

{
    "created_at": "2019-12-18T21:50:47+0000",
    "id": "yhoo/2PYRS",
    "link": "http://yhoo/2PYRS",
    "custom_bitlinks": [],
    "long_url": "http://yahoo.com/",
    "archived": false,
    "tags": [],
    "deeplinks": [],
    "references": {
        "group": "https://api-ssl.bitly.com/v4/groups/yourgroupguid"
    }
}
Run Code Online (Sandbox Code Playgroud)

JSON 响应中的链接值将是您的新 Tiny URL 链接。

  • 非常感谢您提供这个详细的解决方案。自从 V3 停用以来,我一直在到处寻找可靠的文档来迁移到 Bitly API V4。在这里我终于找到了它。 (2认同)