1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | var vimeo_module = require('../index');
|
25 | var Vimeo = vimeo_module.Vimeo;
|
26 | var util_module = require('util');
|
27 |
|
28 | try {
|
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 |
|
36 | var http_module = require('http');
|
37 | var url_module = require('url');
|
38 |
|
39 | var state_data = {
|
40 | state : "unauthorized"
|
41 | };
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | var lib = new Vimeo(config.client_id, config.client_secret);
|
47 |
|
48 | var scopes = ['public', 'private', 'edit', 'interact'];
|
49 | var callback_url = 'http://localhost:8080/oauth_callback';
|
50 |
|
51 |
|
52 | var server = http_module.createServer(function (request, response) {
|
53 | var url = url_module.parse(request.url, true);
|
54 |
|
55 |
|
56 |
|
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 |
|
64 |
|
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 |
|
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 |
|
85 |
|
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 |
|
97 |
|
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 |
|
104 |
|
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 |
|
112 | server.listen(8080, function () {
|
113 | console.log('Server started on 8080');
|
114 | });
|
115 |
|
116 | var context = require('repl').start({}).context;
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 | context.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 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 | context.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 | };
|