集成到 Bit.ly API 版本 4 - 如何生成 OAuth2 令牌以与 Bit.ly API V4 集成并创建缩短的 url? [英] Integrate to Bit.ly API Version 4 - How to Generate OAuth2 Token to integrate with Bit.ly API V4 and create a shortened url?

查看:20
本文介绍了集成到 Bit.ly API 版本 4 - 如何生成 OAuth2 令牌以与 Bit.ly API V4 集成并创建缩短的 url?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

以下是生成通用访问令牌、检索组指南的分步指南,生成 OAuth2 令牌,并调用 Bitly shorten API 请求.

解决方案

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

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

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

单击此链接可参考

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

检索组向导 GET 响应的 JSON 响应:

<代码>{组":[{"创建": "2019-12-18T13:45:21+0000","修改": "2019-12-18T13:45:21+0000","bsds": [],"guid": "XZXUYSJAJ43","organization_guid": "JsjFtE841883","name": "用户名",is_active":真,角色":组织管理员",参考": {组织":https://api-ssl.bitly.com/v4/organizations/JsjFtE841883"}}]}

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

3.) 检索您的 client_id 和 client_secret

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

4.) 生成 OAuth2 访问令牌

请参阅

• 点击发送,您将收到访问令牌.

5.) 缩短网址

• 在 Postman 中打开新选项卡并向 https://api-ssl 发出 Post 请求.bitly.com/v4/shorten

标题应包括:

主机:api-ssl.bitly.com

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

内容类型:application/json

• 您的 正文 应包含此 JSON:

<代码>{"long_url": "http://twitter.com","group_guid": "第 2 步中组 Guid 响应的 guid 字段"}

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

<代码>{"created_at": "2019-12-18T21:50:47+0000","id": "yhoo/2PYRS","link": "http://yhoo/2PYRS","custom_bitlinks": [],"long_url": "http://yahoo.com/",存档":假,标签":[],深层链接":[],参考": {"group": "https://api-ssl.bitly.com/v4/groups/yourgroupguid"}}

JSON 响应中的 link 值将是您的新微型 URL 链接.

I was having the hardest time trying to generate the OAuth2 token to integrate with the Bitly API v4. Their documentation was hard to comprehend and I couldn't find much online or on StackOverflow on how to accomplish it. I was able to get it to work and wanted to share the solution for anyone else who will be scratching their heads in the future.

Below is a step-by-step guide for Generating Generic Access Token, Retrieving the Group Guid, Generating the OAuth2 token, and calling the Bitly shorten API request.

解决方案

Bitly API- Steps to get oAuth2 Access token and using the shorten request to retrieve a Tiny Url using Postman:

After spending the better half of a day trying to figure this out I figured I would post this so it could help someone trying to integrate with Bitly API V4.

Note: Bitly API V3 will be deactivated on March 1st, 2020 so be sure to migrate to V4 as soon as possible. All the guids and other private fields in the responses have been altered for privacy.

Click this link to refer to the Bitly API Documentation

1.) Generate Access Token:

Log into bitly.com with your credentials and click on the Hamburger menu in the top right. Click on your Username and then click on the Generic Access Token. This Generic Access token is needed to retrieve your Group Guid. You will need this Group Guid to make the shorten URL Request along with your access token.

2.) Retrieve Group Guid:

Make a GET Request to https://api-ssl.bitly.com/v4/groups to get your group_guid. You will use your Generic Access Token in the Authorization Field.

Below is a picture how the Headers should look:

You should get a JSON Response back that looks similar to the JSON below:

JSON Response of the Retrieve Group Guid GET Response:

{
    "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"
            }
        }
    ]
}

Your guid value is your group_guid used in your shorten, and other requests.

3.) Retrieve your client_id and client_secret

Register your application by logging into bitly.com with your user credentials and your application will be assigned a client_id and a client_secret. You can accomplish this by clicking on the Hamburger menu on the right side of the page, click on your username, Registered OAuth Apps, and at the bottom their should be a button that says REGISTER NEW APP.

4.) Generate OAuth2 Access Token

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:

Host: api-ssl.bitly.com

Authorization: Bearer (your accesstoken from step 4)

Content-Type: application/json

• Your Body should include this JSON:

{
  "long_url": "http://twitter.com",
  "group_guid": "guid field from Group Guid Response in Step 2"
}

• Click Send and you should see get a JSON Response like this:

{
    "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"
    }
}

The link value in the JSON Response will be your new Tiny URL Link.

这篇关于集成到 Bit.ly API 版本 4 - 如何生成 OAuth2 令牌以与 Bit.ly API V4 集成并创建缩短的 url?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆