As a new developer, I have often heard of APIs, but never fully understood what they were. An Application Programming Interface is a tool that purposefully makes data accessible, and specifically defines how, and in what ways we can interact with this data. APIs usually have thorough documentation that is both technical and practical, detailing many designed user experiences.
作为新开发人员,我经常听说过API,但从未完全了解它们是什么。 应用程序编程接口是一种工具,可有目的地使数据可访问,并专门定义了我们如何以及以何种方式与该数据进行交互。 API通常具有详尽的技术和实践文档,详细介绍了许多设计的用户体验。
REST(REpresentational State Transfer) Client, in our case, is a Ruby gem that acts as an intermediary between us and our API that collects a ‘response’. JSON(JavaScript Object Notation) is another translator, which takes the objects collected by RESTClient from our API request, and ‘parses’ that data, formatting the objects in our response into an easily accessible hash.
REST(RE表象的小号泰特贸易交接)客户端,在我们的情况下,是一个Ruby宝石充当我们和API收集一个“响应”之间的中介。 JSON( 的J ava 小号 CRIPTöbjectÑ浮选)是另一种转换器,它接受从我们的API请求通过RESTClient实现收集的对象,和“解析”的数据,格式化在我们的响应的对象成易于存取的哈希值。
In our project, we decided to allow a user to create playlists dependent on their mood. We needed to import song data from Spotify into a database, so that it would be accessible to a user while our app was functioning. We designed algorithms to choose songs for any given playlist, based on audio feature data and metrics that Spotify assigns to every individual track.
在我们的项目中,我们决定允许用户根据自己的心情创建播放列表。 我们需要将歌曲数据从Spotify导入数据库,以便用户在我们的应用程序运行时可以访问它们。 我们设计了一种算法,可以根据音频功能数据和Spotify分配给每个单独曲目的指标,为任何给定的播放列表选择歌曲。
Log into Spotify’s developer interface, and click the green ‘Create An App’ button. You will be prompted to name and describe your app. When you are finished you will check the boxes, submit, and be redirected to the dashboard that your authorization depends on. Copy and save your ‘Client ID’ and ‘Client Secret’, these are your secret codes to access the API.
登录到Spotify的开发人员界面 ,然后单击绿色的“创建应用”按钮。 系统将提示您命名和描述您的应用。 当你完成,你会检查框,提交,并重定向到您的授权 取决于在仪表盘上 。 复制并保存您的“客户ID”和“客户机密”,这些是您访问API的密码。
The dashboard following this screen holds the secrets you desire 该屏幕后面的仪表板包含您想要的秘密Before you leave the developer dashboard, you will need to white list a redirect URI. You can do this by going to the ‘Edit Settings’ button, and clicking ‘Add’ under Redirect URIs. Using https://example.com/callback/ as our Redirect URI will work for our purposes!
在离开开发人员仪表板之前,您需要将重定向URI列入白名单。 您可以通过以下方法执行此操作:转到“编辑设置”按钮,然后在重定向URI下单击“添加”。 使用https://example.com/callback/作为我们的重定向URI可以达到我们的目的!
Next, we will use the Redirect URI we whitelisted in Step 3 to give us the access token! Copy this URI and paste it into a URL encoder. It should look something like this
接下来,我们将使用在步骤3中列入白名单的重定向URI,为我们提供访问令牌! 复制此URI并将其粘贴到URL编码器中 。 它应该看起来像这样
“https%3A%2F%2Fexample.com%2Fcallback%2F”.
“ https%3A%2F%2Fexample.com%2Fcallback%2F”。
We are now going to do surgery. Spotify’s Implicit Grant Flow tells us we need to use our encoded URI and our client id, formatted into this specific address:
我们现在要去做手术。 Spotify的隐式授权流程告诉我们,我们需要使用编码为以下特定地址的编码URI和客户端ID:
https://accounts.spotify.com/authorize?client_id=YOURCLIENTIDHERE&redirect_uri=YOURENCODEDURIHERE&scope=user-read-private%20user-read-email&response_type=token&state=123
https://accounts.spotify.com/authorize?client_id=YOURCLIENTIDHERE&redirect_uri=YOURENCODEDURIHERE&scope=user-read-private%20user-read-email&response_type=token&state=123
My address, after using my Client ID and URI, looked like this:
使用我的客户ID和URI后,我的地址如下所示:
https://accounts.spotify.com/authorize?client_id=8449a8d2ab9b40f8b14324f3a22a368d&redirect_uri=https%3A%2F%2Fexample.com%2Fcallback%2F&scope=user-read-private%20user-read-email&response_type=token&state=123
https://accounts.spotify.com/authorize?client_id= 8449a8d2ab9b40f8b14324f3a22a368d &redirect_uri = https%3A%2F%2Fexample.com%2Fcallback%2F &scope = user-read-private%20user-read-email&response_type = token&state =
We now, are going to paste this constructed address into our browser. We will be directed to a Spotify page to authorize our app!
现在,我们将把这个构造的地址粘贴到我们的浏览器中。 我们将被定向到Spotify页面以授权我们的应用!
Spotify App Authorization Spotify应用授权We are not done here! Don’t close this web page yet!
我们还没有完成! 不要关闭此网页!
If you look in the address bar, you will see a peculiar web address, hopefully the one you whitelisted in your Spotify dashboard… however… it also includes our very own Access Token!!!
如果您在地址栏中查看,您将看到一个特殊的网址,希望您在Spotify仪表板中将其列入白名单。但是……它也包含我们自己的访问令牌!
https://example.com/callback/#access_token=BQB8Yobrgz4Uagd3DFs8qGjHcDiIs0g6skWm8phGn1AcncYXcfdXDpzqvNanBcH2Y0BCZUNGzB57gn0O8dD01J62kCYcJ85XcHKCmw5AI7oShU8W7osmfyAcQMKGlr0zgsp_fPNYY3qu7Fc_XSVlUid0Xc4&token_type=Bearer&expires_in=3600&state=123
https://example.com/callback/#access_token=BQB8Yobrgz4Uagd3DFs8qGjHcDiIs0g6skWm8phGn1AcncYXcfdXDpzqvNanBcH2Y0BCZUNGzB57gn0O8dD01J62kCYcJ85XcHKCmw5AI7oShU8W7osmfyAcQMKGlr0zgsp_fPNYY3qu7Fc_XSVlUid0Xc4&token_type=Bearer&expires_in=3600&state=123
Copy and save the Access Token, which includes everything after #access_token= and before &token_type=Bearer. This token type is important to note, we will also use this later on.
复制并保存访问令牌,其中包括#access_token =之后和&token_type = Bearer之前的所有内容。 此标记类型很重要,请注意,我们稍后还将使用它。
NOTE: Access Tokens will only work for 60 minutes, if the token expires, you can repeat this step for an additional 60 minutes of access.
注意:访问令牌只能使用60分钟,如果令牌过期,您可以重复此步骤再访问60分钟。
Now, we need to create the app! I’ll use this boilerplate CLI design, and call it “medium_cli_app”. We next need to add the gem ‘rest-client’ to our Gemfile and bundle install. We are using ActiveRecord and Rake to seed data into a database, so we will also need these gems included, however I already had them in my template. Within our db/seeds.rb we will need to require both ‘json’ and ‘rest-client’.
现在,我们需要创建该应用程序! 我将使用此样板CLI设计 ,并将其称为“ medium_cli_app”。 接下来,我们需要将gem'rest-client'添加到我们的Gemfile并捆绑安装。 我们正在使用ActiveRecord和Rake将数据播种到数据库中,因此我们也需要包含这些gems,但是我已经在模板中添加了它们。 在我们的db / seeds.rb文件中,我们将需要同时使用“ json”和“ rest-client”。
Don’t forget to save! 不要忘记保存!Back in our app seeds.rb file, we are going to create an “auth” variable so that we can easily pass it into our GET requests. This structure will be a hash, with the key “Authorization” and the value of your Access Token type, followed by your Access Token as a single string.
回到我们的应用程序seed.rb文件中,我们将创建一个“ auth”变量,以便我们可以轻松地将其传递到GET请求中。 该结构将是一个哈希,具有键“ Authorization”和您的访问令牌类型的值,然后是您的访问令牌作为单个字符串。
auth = {“Authorization”: “Bearer BQDc2NzoRj2M6I4WPRdYdjNsyzrhtYP1cPq4fJge44dQvpg90hOab6aRPylW8IZONZwMM1jh3d5ffapk1FsMcV9gsrBFIyu3U2b0–3u__8XvFOr0D7p_iqF3uEFr-VI7DoFcW3bBsI_2fE1fntrjZ97buywackYoEYc”}
AUTH = {“授权”:“承载BQDc2NzoRj2M6I4WPRdYdjNsyzrhtYP1cPq4fJge44dQvpg90hOab6aRPylW8IZONZwMM1jh3d5ffapk1FsMcV9gsrBFIyu3U2b0-3u__8XvFOr0D7p_iqF3uEFr-VI7DoFcW3bBsI_2fE1fntrjZ97buywackYoEYc”}
Next, we will use a RestClient.get request, pass a Spotify API Endpoint in, and as a second arguement, our auth variable set as a header.
接下来,我们将使用RestClient.get请求,在其中传递一个Spotify API端点,作为第二个论点,将我们的auth变量设置为标头。
endpoint1 = RestClient.get(
endpoint1 = RestClient.get(
‘https://api.spotify.com/v1/browse/categories/mood/playlists?offset=0&limit=50',
'https://api.spotify.com/v1/browse/categories/mood/playlists?offset=0&limit=50',
headers=auth)
标头= auth )
Last, we will use a JSON.parse() method to parse the variable we set our RestClient.get response to.
最后,我们将使用JSON.parse()方法来解析我们设置RestClient.get响应的变量。
data1 = JSON.parse(endpoint1)
data1 = JSON.parse( endpoint1 )
Our final product, will look similar to this in our seeds.rb file:
我们的最终产品在seed.rb文件中看起来与此类似:
We are almost there!!! 我们就快到了!!!Here is where you come in! Once we have made authorized our GET requests, we can more effectively utilize Spotify’s Web API documentation to determine endpoints we wish to access. We can preform endless methods on this collected data and use endless methods to seed it into our database. I will show a couple examples of how I used this access to parse, collect, and persist data points into my database.
这是您进来的地方! 授权我们的GET请求后,我们可以更有效地利用Spotify的Web API文档来确定我们希望访问的端点。 我们可以对收集到的数据执行无穷尽的方法,并使用无穷尽的方法将其植入我们的数据库。 我将展示几个示例,说明如何使用此访问权限将数据点解析,收集和持久保存到数据库中。
This is not a comprehensive guide on API authorization. What this method allowed me to do, is overcome the barrier of entry into the wealth of value APIs can offer. I frantically searched Google, Medium, Youtube, GitHub, and tried various Ruby gems specifically designed for accessing Spotify’s API but could not find a step by step approach (with images) how to be granted access. This is a jumping off place, I look forward to learning and understanding more dynamic ways of utilizing these tools. After my experience, I have deepened my respect for the traditional documents that as developers we often find ourselves. If you need any additional help, or would just like a person to walk through authorization with, reach out to me and I will see if I can be of some use to you!
这不是有关API授权的全面指南。 这种方法允许我做的是,克服了进入API可以提供的大量价值的障碍。 我疯狂地搜索了Google,Medium,Youtube,GitHub,并尝试了各种专门用于访问Spotify API的Ruby gem,但找不到逐步的方法(带有图像)如何授予访问权限。 这是一个起点,我期待学习和理解使用这些工具的更多动态方法。 根据经验,我加深了对作为开发人员经常会遇到的传统文档的尊重。 如果您需要任何其他帮助,或者只想一个人来了解授权,请与我联系,我会看看我是否对您有帮助!
We will link our finished project after it has been pushed to GitHub for production!
将已完成的项目推送到GitHub进行生产后,我们将其链接!
HUGE shoutout to Jake Fromm, my partner for this project and my cohort who all encourage me to become a more well rounded developer!
向我的这个项目的合作伙伴和我的同伙杰克·弗洛姆 ( Jake Fromm)大喊大叫 ,他们都鼓励我成为一个更加全面的开发人员!
翻译自: https://medium.com/swlh/how-to-access-spotifys-web-api-using-ruby-restclient-and-json-c04f8f8f23cd
相关资源:四史答题软件安装包exe