UNPKG

2.56 kBJavaScriptView Raw
1"use strict";
2/**
3 * Copyright 2014 Vimeo
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18var Vimeo = require('../index').Vimeo;
19
20try {
21 var config = require('./config.json');
22} catch (error) {
23 console.error('ERROR: For this example to run properly you must create an api app at '
24 + 'developer.vimeo.com/apps/new and set your callback url to '
25 + 'http://localhost:8080/oauth_callback');
26 console.error('ERROR: Once you have your app, make a copy of config.json.example named '
27 + '"config.json" and add your client id, client secret and access token');
28}
29
30function makeRequest(lib) {
31 // Make an API request
32 lib.request({
33 // This returns the first page of videos containing the term "myquery".
34 // These videos will be sorted by most relevant to least relevant
35 path : '/videos',
36 query : {
37 page : 1,
38 per_page : 10,
39 query : 'myquery',
40 sort : 'relevant',
41 direction : 'asc'
42 }
43 }, function (error, body, status_code, headers) {
44 if (error) {
45 console.log('error');
46 console.log(error);
47 } else {
48 console.log('body');
49 console.log(body);
50 }
51
52 console.log('status code');
53 console.log(status_code);
54 console.log('headers');
55 console.log(headers);
56 });
57}
58
59var lib = new Vimeo(config.client_id, config.client_secret);
60
61if (config.access_token) {
62 lib.access_token = config.access_token;
63 makeRequest(lib);
64} else {
65 // Unauthenticated api requests must request an access token. You should not request a new
66 // access token for each request, you should request an access token once and use it over
67 // and over.
68 lib.generateClientCredentials('public', function (err, access_token) {
69 if (err) {
70 throw err;
71 }
72
73 // Assign the access token to the library
74 lib.access_token = access_token.access_token;
75 makeRequest(lib);
76 });
77}