diff options
| author | Mohammad Amoush <mohammad_amoush@brown.edu> | 2019-06-26 15:14:46 -0400 |
|---|---|---|
| committer | Mohammad Amoush <mohammad_amoush@brown.edu> | 2019-06-26 15:14:46 -0400 |
| commit | b86050edd2da3acca258f117e8350aa8d53272d9 (patch) | |
| tree | ad2ceebb8e8d2f0c9d57f13d125241e96de6f944 /src/client/apis | |
| parent | b285803c4e8c37302f6e02624a6127667d628305 (diff) | |
Sample working onserver side
Diffstat (limited to 'src/client/apis')
| -rw-r--r-- | src/client/apis/youtube/YoutubeBox.tsx | 9 | ||||
| -rw-r--r-- | src/client/apis/youtube/youtubeApiSample.d.ts | 3 | ||||
| -rw-r--r-- | src/client/apis/youtube/youtubeApiSample.js | 122 |
3 files changed, 2 insertions, 132 deletions
diff --git a/src/client/apis/youtube/YoutubeBox.tsx b/src/client/apis/youtube/YoutubeBox.tsx index ee190750f..b029c51ec 100644 --- a/src/client/apis/youtube/YoutubeBox.tsx +++ b/src/client/apis/youtube/YoutubeBox.tsx @@ -7,7 +7,6 @@ import { observer } from "mobx-react"; import { computed, reaction, IReactionDisposer } from 'mobx'; import { DocumentDecorations } from "../../views/DocumentDecorations"; import { InkingControl } from "../../views/InkingControl"; -import * as YoutubeApi from "./youtubeApiSample"; import { Utils } from "../../../Utils"; import { DocServer } from "../../DocServer"; @@ -15,14 +14,11 @@ import { DocServer } from "../../DocServer"; @observer export class YoutubeBox extends React.Component<FieldViewProps> { - private youtubeApiKey: string = ""; public static LayoutString() { return FieldView.LayoutString(YoutubeBox); } - async componentWillMount() { - let apiKey = await DocServer.getYoutubeApiKey(); - this.youtubeApiKey = apiKey; - YoutubeApi.authorizedGetChannel(this.youtubeApiKey); + componentWillMount() { + DocServer.getYoutubeChannels(); } _ignore = 0; @@ -45,7 +41,6 @@ export class YoutubeBox extends React.Component<FieldViewProps> { render() { let field = this.props.Document[this.props.fieldKey]; let view; - YoutubeApi.readFsFile(); if (field instanceof HtmlField) { view = <span id="webBox-htmlSpan" dangerouslySetInnerHTML={{ __html: field.html }} />; } else if (field instanceof WebField) { diff --git a/src/client/apis/youtube/youtubeApiSample.d.ts b/src/client/apis/youtube/youtubeApiSample.d.ts deleted file mode 100644 index 87a669e36..000000000 --- a/src/client/apis/youtube/youtubeApiSample.d.ts +++ /dev/null @@ -1,3 +0,0 @@ - -declare const YoutubeApi: any; -export = YoutubeApi;
\ No newline at end of file diff --git a/src/client/apis/youtube/youtubeApiSample.js b/src/client/apis/youtube/youtubeApiSample.js deleted file mode 100644 index 7f14f2d3e..000000000 --- a/src/client/apis/youtube/youtubeApiSample.js +++ /dev/null @@ -1,122 +0,0 @@ -import { Utils } from "tslint"; - - -// If modifying these scopes, delete your previously saved credentials -// at ~/.credentials/youtube-nodejs-quickstart.json -let SCOPES = ['https://www.googleapis.com/auth/youtube.readonly']; -let TOKEN_DIR = (process.env.HOME || process.env.HOMEPATH || - process.env.USERPROFILE) + '/.credentials/'; -let TOKEN_PATH = TOKEN_DIR + 'youtube-nodejs-quickstart.json'; - - - -function authorizedGetChannel(apiKey) { - // Authorize a client with the loaded credentials, then call the YouTube API. - authorize(JSON.parse(apiKey), getChannel); -} - - -/** - * Create an OAuth2 client with the given credentials, and then execute the - * given callback function. - * - * @param {Object} credentials The authorization client credentials. - * @param {function} callback The callback to call with the authorized client. - */ -function authorize(credentials, callback) { - let clientSecret = credentials.installed.client_secret; - let clientId = credentials.installed.client_id; - let redirectUrl = credentials.installed.redirect_uris[0]; - let oauth2Client = new OAuth2(clientId, clientSecret, redirectUrl); - - // Check if we have previously stored a token. - fs.readFile(TOKEN_PATH, function (err, token) { - if (err) { - getNewToken(oauth2Client, callback); - } else { - oauth2Client.credentials = JSON.parse(token); - callback(oauth2Client); - } - }); -} - -/** - * Get and store new token after prompting for user authorization, and then - * execute the given callback with the authorized OAuth2 client. - * - * @param {google.auth.OAuth2} oauth2Client The OAuth2 client to get token for. - * @param {getEventsCallback} callback The callback to call with the authorized - * client. - */ -function getNewToken(oauth2Client, callback) { - var authUrl = oauth2Client.generateAuthUrl({ - access_type: 'offline', - scope: SCOPES - }); - console.log('Authorize this app by visiting this url: ', authUrl); - var rl = readline.createInterface({ - input: process.stdin, - output: process.stdout - }); - rl.question('Enter the code from that page here: ', function (code) { - rl.close(); - oauth2Client.getToken(code, function (err, token) { - if (err) { - console.log('Error while trying to retrieve access token', err); - return; - } - oauth2Client.credentials = token; - storeToken(token); - callback(oauth2Client); - }); - }); -} - -/** - * Store token to disk be used in later program executions. - * - * @param {Object} token The token to store to disk. - */ -function storeToken(token) { - try { - fs.mkdirSync(TOKEN_DIR); - } catch (err) { - if (err.code != 'EEXIST') { - throw err; - } - } - fs.writeFile(TOKEN_PATH, JSON.stringify(token), (err) => { - if (err) throw err; - console.log('Token stored to ' + TOKEN_PATH); - }); - console.log('Token stored to ' + TOKEN_PATH); -} - -/** - * Lists the names and IDs of up to 10 files. - * - * @param {google.auth.OAuth2} auth An authorized OAuth2 client. - */ -function getChannel(auth) { - var service = google.youtube('v3'); - service.channels.list({ - auth: auth, - part: 'snippet,contentDetails,statistics', - forUsername: 'GoogleDevelopers' - }, function (err, response) { - if (err) { - console.log('The API returned an error: ' + err); - return; - } - var channels = response.data.items; - if (channels.length == 0) { - console.log('No channel found.'); - } else { - console.log('This channel\'s ID is %s. Its title is \'%s\', and ' + - 'it has %s views.', - channels[0].id, - channels[0].snippet.title, - channels[0].statistics.viewCount); - } - }); -}
\ No newline at end of file |
