UNPKG

7.18 kBJavaScriptView Raw
1"use strict";
2
3/**
4 * Copyright 2013 Vimeo
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19
20/**
21 * For this example to run properly you must create an api app at developer.vimeo.com/apps/new and set your callback url to http://localhost:8080/oauth_callback
22 *
23 */
24var vimeo_module = require('../index');
25var Vimeo = vimeo_module.Vimeo;
26var util_module = require('util');
27
28try {
29 var config = require('./config.json');
30} catch (error) {
31 console.error('ERROR: For this example to run properly you must create an api app at developer.vimeo.com/apps/new and set your callback url to http://localhost:8080/oauth_callback');
32 console.error('ERROR: Once you have your app, make a copy of config.json.example named "config.json" and add your client id and client secret');
33 return;
34}
35
36var http_module = require('http');
37var url_module = require('url');
38
39var state_data = {
40 state : "unauthorized"
41};
42
43// Here we have to build the vimeo library using the client_id and client_secret
44// We do not need an access token here because we will generate one
45// If we already knew our access token, we can provide it as the third parameter
46var lib = new Vimeo(config.client_id, config.client_secret);
47
48var scopes = ['public', 'private', 'edit', 'interact'];
49var callback_url = 'http://localhost:8080/oauth_callback';
50
51// The authorization process requires the user to be redirected back to a webpage, so we can start up a simple http server here
52var server = http_module.createServer(function (request, response) {
53 var url = url_module.parse(request.url, true);
54
55 // Once the user accepts your app, they will be redirected back to localhost:8080/oauth_callback.
56 // If they are not redirected you should check your apps configuration on developer.vimeo.com/apps
57 if (url.pathname === '/oauth_callback') {
58 if (url.query.state !== 'abcdefg') {
59 throw new Error('invalid state');
60 }
61
62 if (!url.query.error) {
63 // At this state (a request to /oauth_callback without an error parameter)
64 // the user has been redirected back to the app and you can exchange the "code" parameter for an access token
65 console.info('successful oauth callback request');
66 lib.accessToken(url.query.code, callback_url, function (err, token) {
67 if (err) {
68 return response.end("error\n" + err);
69 }
70
71 if (token.access_token) {
72 // At this state the code has been successfully exchanged for an access token
73 lib.access_token = token.access_token;
74 state_data.user = token.user;
75 state_data.state = "authorized";
76 response.statusCode = 302;
77 response.setHeader('Location', '/');
78 response.end();
79 } else {
80 throw new Error('no access token provided');
81 }
82 });
83 } else {
84 // At this state (a request to /oauth_callback with an error parameter)
85 // something went wrong when you sent your user to Vimeo.com. The error parameter should tell you more
86 console.error('failed oauth callback request');
87 console.error(url.query.error);
88
89 response.setHeader('Content-Type', 'text/html');
90 response.write('Your command line is currently unauthenticated. Please ');
91 response.end('<a href="' + lib.buildAuthorizationEndpoint(callback_url, scopes, 'abcdefg') + '">Link with Vimeo</a><br />' + JSON.stringify(url.query));
92 }
93
94 } else {
95 if (state_data.state !== "authorized") {
96 // At this state (any request where state_data.state has not been set to "authorized")
97 // we do not have an authentication token, so we need to send the user to Vimeo.
98 console.info('http request without access token');
99 response.setHeader('Content-Type', 'text/html');
100 response.write('Your command line is currently unauthenticated. Please ');
101 response.end('<a href="' + lib.buildAuthorizationEndpoint(callback_url, scopes, 'abcdefg') + '">Link with Vimeo</a>');
102 } else {
103 // At this state (state_data.state has been set to "authorized" when we retrieved the access token)
104 // we can make authenticated api requests
105 console.info('http request with access token');
106 response.setHeader('Content-Type', 'text/html');
107 response.end('Your command line is currently authorized with the user : <a href="' + state_data.user.link + '">' + state_data.user.name + '</a>. You can make api requests via the command line using the "request" function, or upload files using the "upload" function.<br /> Try "request(\'/me\');"');
108 }
109 }
110});
111
112server.listen(8080, function () {
113 console.log('Server started on 8080');
114});
115
116var context = require('repl').start({}).context;
117
118/**
119 * This will upload the video to the authenticated account.
120 *
121 * @param {string} path The path to the video file
122 * @param {string} video_uri Optional. If provided, this upload will replace the source file of the video uri provided
123 */
124context.upload = function (path, video_uri) {
125 lib.streamingUpload(path, video_uri, function (err, data, status, headers) {
126 if (err) {
127 console.log('---upload error---');
128 console.log('error');
129 console.log(err);
130 console.log('response body');
131 console.log(data);
132 console.log('response status');
133 console.log(status);
134 console.log('response headers');
135 console.log(headers);
136 } else {
137 console.log('---upload success---');
138 console.log('response body');
139 console.log(data);
140 console.log('response status');
141 console.log(status);
142 console.log('response headers');
143 console.log(headers);
144 }
145 });
146};
147
148/**
149 * This method lets you make api requests.
150 *
151 * options.method is a string of the HTTP method for the request (GET, POST, PUT, PATCH, DELETE)
152 * options.path is a string of the path portion of a url (eg. /users/dashron)
153 * options.query is an object containing all of your request parameters. If GET they will be appended to the url, if POST it will be part of your request body
154 * options.headers is an object containing key value pairings of all of the HTTP request headers
155 *
156 * @param {Object|String} options If string, it will make a GET request to that url. If an object, you can provide many parameters. See the function description for more.
157 */
158context.request = function (options) {
159 if (typeof options === "string") {
160 options = {path : options};
161 }
162
163 lib.request(options, function (err, data, status, headers) {
164 if (err) {
165 console.log('---request error---');
166 console.log('status');
167 console.log(status);
168 console.log('headers');
169 console.log(headers);
170
171 console.log('error');
172 console.log(util_module.inspect(err));
173 } else {
174 console.log('---request success---');
175 console.log('status');
176 console.log(status);
177 console.log('headers');
178 console.log(headers);
179
180 console.log('response');
181 console.log(data);
182 }
183 });
184};