Last active
October 29, 2015 22:42
-
-
Save terrysahaidak/b7f9aea679de2dc7e82e to your computer and use it in GitHub Desktop.
vk webapi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//import fetch from 'isomorphic-fetch' | |
import qs from 'query-string' | |
const remote = window.require('remote') | |
const BrowserWindow = remote.require('browser-window') | |
const fetch = remote.require('isomorphic-fetch') | |
export default class WebApi { | |
constructor(args={}) { | |
let defaultConfig = this._getDefaultConfig() | |
this._config = {} | |
this._baseUrl = defaultConfig['base_url'] | |
this._config['client_id'] = args['client_id'] || defaultConfig['client_id'] | |
this._config['v'] = args['v'] || defaultConfig['v'] | |
this._config['scope'] = args['scope'] || defaultConfig['scope'] | |
this._config['redirect_uri'] = args['redirect_uri'] || defaultConfig['redirect_uri'] | |
this._config['display'] = args['display'] || defaultConfig['display'] | |
this._config['response_type'] = args['response_type'] || defaultConfig['response_type'] | |
this._baseApiCallUrl = 'https://api.vk.com/method/' | |
} | |
loginUser() { | |
return new Promise( (resolve, reject) => { | |
if (!window.sessionStorage.getItem('access_token')) { | |
const newWindow = new BrowserWindow({ | |
width: 1050, | |
height: 550, | |
'auto-hide-menu-bar': true | |
}) | |
newWindow.loadUrl(this._getFullAuthUrl()) | |
newWindow.webContents.on('did-finish-load', () => { | |
var params = qs.parse(newWindow.webContents.getUrl().split('#')[1]) | |
if ('access_token' in params) { | |
this._saveToken(params['access_token']) | |
newWindow.close() | |
resolve() | |
} | |
newWindow.on('close', () => { | |
if (!window.sessionStorage.getItem('access_token')) { | |
reject(new Error('Login was canceled')) | |
} | |
}) | |
}) | |
} else { | |
reject(new Error('User already logined in')) | |
} | |
}) | |
} | |
checkLoginStatus() { | |
if (!!window.sessionStorage.getItem('access_token')) | |
return true | |
else | |
return false | |
} | |
logoutUser() { | |
try { | |
const currentWindow = remote.getCurrentWebContents() | |
currentWindow.session.clearStorageData(['cookies'], (error, cookies) => { | |
console.log(cookies); | |
}); | |
window.sessionStorage.removeItem('access_token'); | |
} catch(error) { | |
throw new Error('something bad happents: ', error) | |
} | |
} | |
getAuthedUser() { | |
let access_token = this._getToken() | |
let query = qs.stringify({ | |
fields: 'photo_400_orig,counters', | |
access_token | |
}) | |
return fetch(`${this._baseApiCallUrl}users.get?${query}`) | |
} | |
getFriends(order='hints', name_case='nom', fields='photo_100') { | |
let access_token = this._getToken() | |
let query = qs.stringify({ | |
order, | |
name_case, | |
fields, | |
access_token | |
}) | |
return fetch(`${this._baseApiCallUrl}friends.get?${query}`) | |
} | |
/*===============================================================*/ | |
/*======================== Private ==============================*/ | |
/*===============================================================*/ | |
_getDefaultConfig() { | |
return { | |
'base_url': 'https://oauth.vk.com/authorize', | |
'client_id': '5120154', | |
'v': 'v=5.37', | |
'scope': '2810242621444096', | |
'redirect_uri': 'https://oauth.vk.com/blank.html', | |
'display': 'page', | |
'response_type': 'token', | |
'full_ulr': 'https://oauth.vk.com/authorize?client_id=5120154&v=5.37&redirect_uri=https://oauth.vk.com/blank.html&display=page&response_type=token&scope=2810242621444096' | |
} | |
} | |
_getFullAuthUrl() { | |
let query = qs.stringify(this._config) | |
return `${this._baseUrl}?${query}` | |
} | |
_saveToken(token) { | |
window.sessionStorage.setItem('access_token', token) | |
} | |
_getToken() { | |
return window.sessionStorage.getItem('access_token') | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment