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

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

问题描述

我最难以尝试生成 OAuth2令牌 以与 Bitly API v4 集成。他们的文档很难理解,我无法在网上或在StackOverflow上找到太多有关如何完成此文档的信息。我能够使它正常工作,并希望与其他将来挠头的人分享该解决方案。



以下是生成 Generic Access Token (获取 Group Guid 生成OAuth2令牌 ,然后调用Bitly shortening API请求。

解决方案

位API -获取 oAuth2访问权限的步骤令牌并使用缩短请求使用邮递员



花了一天多的时间尝试弄清楚这一点后,我想我会发布此消息,以便它可以帮助尝试与 Bitly API V4 集成的人。



注意: Bitly API V3将于2020年3月1日停用,因此请确保尽快迁移到V4尽可能。



响应中的所有Guid和其他私有字段均已更改,以保护隐私。单击此链接以引用< a href = https://dev.bitly.com/v4_documentation.html rel = noreferrer>位API文档



1.)生成访问令牌:



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



2。)检索Group Guid:



进行 GET 请求以



您应该得到一个类似于以下JSON的JSON响应:



检索组的JSON响应Guid GET响应:

  {
:[[
{
created: 2019-12-18T13:45:21 + 0000,
modified: 2019-12-18T13:45:21 + 0000 ,
bsds:[],
guid: XZXUYSJAJ43,
organization_guid: JsjFtE841883,
name:用户名,
is_active:true,
role: org-admin,
references:{
organization: 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。)缩短URL



•在Postman中打开新标签页并进行向 https://api-ssl.bitly.com/v4/shortening 发送请求



标题应包括:



主机 api-ssl.bitly.com



授权 Bearer(您从第4步获得的访问令牌)



内容类型 application / json



•您的正文应包含此JSON:

  {
long_url: http:// twitter .com,
group_guid:步骤2中来自Group Guid Response的Guid字段
}

•单击发送,您应该会看到类似以下的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,
标签:[],
深层链接:[],
参考:{
组: https:// api-ssl。 bitly.com/v4/groups/yourgroupguid
}
}

JSON响应中的 link 值将是您的新Tiny 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.

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

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