600px 中文文档教程

发布于 8年前 浏览 35 项目主页 更新于 3年前

600px

 npmdependencies

500px.com 包装器,比现有的 500px 节点模块:眨眼:。 包装 500px API,公开一个干净的、经过良好测试的、基于承诺的接口,支持经过身份验证的请求。

Installation

$ npm install 600px

Usage

像往常一样将它要求到您的项目中。 主模块是一个需要正确初始化配置对象的类。 您需要一个用于公共方法的 consumer_key,以及用于经过身份验证的方法的更多信息,我们将在下一节中讨论。

var API = require('600px'),
    api = new API({ consumer_key: 'xxx' });

现在您有一个可用于每个 API 端点的对象,如下所示:

var Photos = api.photos;
var Users = api.users;
var Blogs = api.blogs;
var Collections = api.collections;

有许多方法可以在每个端点上发出特定的 API 请求,如下所述。 目前,仅支持 GET 方法,尽管我计划添加对 POSTPUT 方法的支持,并且架构已经到位。

Authorization

该库仅供个人使用,因此不包含完整的授权流程。 如果确实需要授权,则必须通过OAuth在浏览器中进行,可以使用官方维护的500px客户端js库为此。

不幸的是,500px 无法让您轻松获得个人访问令牌,因此您需要了解整个 OAuth 并制作一个虚拟应用程序,以获取您需要的信息来发出经过身份验证的请求。 您可以采取以下一组步骤来相当轻松地获取个人访问令牌:

  • Make a new 500px application, make sure to set the callback url to https://grant-oauth.herokuapp.com/connect/500px/callback.
  • Head over to the wonderful grant oauth playground, check the app box, and enter in your application's consumer key and secret which you just generated
  • Go through the oauth flow. At the end, you will be redirected to grant's playground where you will get a access token and access secret.
  • In order to make authenticated calls, you need a consumer_key, consumer_secret, token, and token_secret, which you now have all of. Initialize this library's class with those four, named appropriately, and you will be able to make authenticated calls.

完成此操作后,您可以无误地访问任何端点。 如果没有这个,在 500px 的 API 文档 中指示它们需要 OAuth 的端点将返回错误。

Photos

要检索照片,请使用下面列出的任何方法在实例化类上使用 photos。 一个简短的例子:

api.photos.getPopular({'sort': 'created_at', 'rpp': '100'})
  .catch(console.error)
  .then(console.log);

请注意,每个方法都会返回一个由 when.js 生成的符合 A+ 标准的承诺。 如果您不知道如何使用 promise,请不要担心,基础知识非常简单。 您可以按照上面的示例进行操作:使用 then 在成功时执行一个函数,在错误时使用 catch。 以下是您可以使用的所有与照片相关的功能:

  • getById (photo_id, arguments)
  • getByUsername (username, arguments)
  • getByUserId (user_id, arguments)
  • getFavoritesByUsername (username, arguments)
  • getFavoritesByUserId (user_id, arguments)
  • getFriendsByUsername (username, arguments)
  • getFriendsByUserId (user_id, arguments)
  • getPopular (arguments)
  • getUpcoming (arguments)
  • getEditorsChoice (arguments)
  • getFreshToday (arguments)
  • getFreshYesterday (arguments)
  • getFreshWeek (arguments)
  • searchByTag (tag, arguments)
  • searchByTerm (term, arguments)

有关可能参数的更多信息,请查看 500px.com 照片 API

Users

示例:

api.users.getById(1234)
  .catch(console.error)
  .then(console.log);

可用函数:

  • get (arguments)
  • getById (id, arguments)
  • getByName (username, arguments)
  • getByEmail (email, arguments)
  • getFriendsById (id, arguments)

有关可能参数的更多信息,请查看 500px.com 用户 API

Blogs

示例:

api.blogs.getFresh()
  .catch(console.error)
  .then(console.log);

函数:

  • getById (id, arguments)
  • getCommentsById (id, arguments)
  • getFresh (arguments)
  • getByUsername (username, arguments)
  • getByUserId (user_id, arguments)

有关可能参数的更多信息,请查看 500px.com 博客 API

Collections

请注意所有集合端点需要完整的身份验证凭据。

示例:

api.collections.get()
  .catch(console.error)
  .then(console.log);

函数:

  • get (arguments)
  • getById (id, arguments)

有关可能参数的更多信息,请查看

License

MIT 许可的 500px.com collections API查看许可证 »)

600px

npmdependencies

A 500px.com wrapper that does a little more than the existing 500px node module :wink:. Wraps the 500px API, exposing a clean, well-tested, promise-based interface with support for authenticated requests.

Installation

$ npm install 600px

Usage

Require it into your project as usual. The main module is a class that requires a configuration object to be correctly initialized. You need a consumer_key for the public methods, and a bit more information for the authenticated methods, which we'll talk about in the next section.

var API = require('600px'),
    api = new API({ consumer_key: 'xxx' });

Now you have an object available for each API endpoint, as such:

var Photos = api.photos;
var Users = api.users;
var Blogs = api.blogs;
var Collections = api.collections;

There are a number of methods to make specific API requests on each of these endpoints, documented below. At the moment, only GET methods are supported, although I plan on adding support for POST and PUT methods down the line as well, and the architecture is in place for it.

Authorization

This library is intended for personal use and therefore does not include a full authorization flow. If you do need to authorize, it has to happen in the browser through OAuth, and you can use the officially maintained 500px client-side js library for this.

Unfortunately, 500px offers no way for you to easily obtain a personal access token, so you need to go through the whole OAuth thing and make a dummy app in order to get the info you need to make authenticated requests. Here are a set of steps you can take to get personal access tokens fairly painlessly:

  • Make a new 500px application, make sure to set the callback url to https://grant-oauth.herokuapp.com/connect/500px/callback.
  • Head over to the wonderful grant oauth playground, check the app box, and enter in your application's consumer key and secret which you just generated
  • Go through the oauth flow. At the end, you will be redirected to grant's playground where you will get a access token and access secret.
  • In order to make authenticated calls, you need a consumer_key, consumer_secret, token, and token_secret, which you now have all of. Initialize this library's class with those four, named appropriately, and you will be able to make authenticated calls.

With this done, you can hit any endpoint without errors. Without this in place, the endpoints that indicate that they require OAuth in 500px's API docs will return an error.

Photos

To retrieve photos, use photos on the instantiated class with any of the methods listed below. A brief example:

api.photos.getPopular({'sort': 'created_at', 'rpp': '100'})
  .catch(console.error)
  .then(console.log);

Note that each method returns a A+ compliant promise generated by when.js. If you don't know how to use promises, don't worry at all, the basics are super easy. You can just follow the example as above: use then to execute a function on success and catch on error. Below are all the photo-related functions you can use:

  • getById (photo_id, arguments)
  • getByUsername (username, arguments)
  • getByUserId (user_id, arguments)
  • getFavoritesByUsername (username, arguments)
  • getFavoritesByUserId (user_id, arguments)
  • getFriendsByUsername (username, arguments)
  • getFriendsByUserId (user_id, arguments)
  • getPopular (arguments)
  • getUpcoming (arguments)
  • getEditorsChoice (arguments)
  • getFreshToday (arguments)
  • getFreshYesterday (arguments)
  • getFreshWeek (arguments)
  • searchByTag (tag, arguments)
  • searchByTerm (term, arguments)

For more information on the possible arguments, please check the 500px.com photos API

Users

Example:

api.users.getById(1234)
  .catch(console.error)
  .then(console.log);

Available functions:

  • get (arguments)
  • getById (id, arguments)
  • getByName (username, arguments)
  • getByEmail (email, arguments)
  • getFriendsById (id, arguments)

For more information on the possible arguments, please check the 500px.com users API

Blogs

Example:

api.blogs.getFresh()
  .catch(console.error)
  .then(console.log);

Functions:

  • getById (id, arguments)
  • getCommentsById (id, arguments)
  • getFresh (arguments)
  • getByUsername (username, arguments)
  • getByUserId (user_id, arguments)

For more information on the possible arguments, please check the 500px.com blogs API

Collections

Note that all collections endpoints require full authentication credentials.

Example:

api.collections.get()
  .catch(console.error)
  .then(console.log);

Functions:

  • get (arguments)
  • getById (id, arguments)

For more information on the possible arguments, please check the 500px.com collections API

License

Licensed under MIT (see license »)

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文