-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.ios.js
104 lines (95 loc) · 2.47 KB
/
index.ios.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
'use strict';
var React = require('react-native')
var shittyQs = require('shitty-qs')
var config = require('./config.js')
var {
AppRegistry,
StyleSheet,
Text,
View,
TouchableHighlight,
LinkingIOS
} = React
function dropboxOauth (app_key, callback) {
var state = Math.random() + ''
LinkingIOS.addEventListener('url', handleUrl)
function handleUrl (event) {
var [, query_string] = event.url.match(/\#(.*)/)
var query = shittyQs(query_string)
if (state === query.state) {
callback(null, query.access_token, query.uid)
} else {
callback(new Error('Oauth2 security error'))
}
LinkingIOS.removeEventListener('url', handleUrl)
}
LinkingIOS.openURL([
'https://www.dropbox.com/1/oauth2/authorize',
'?response_type=token',
'&client_id=' + app_key,
'&redirect_uri=oauth2example://foo',
`&state=${state}`
].join(''))
}
var OauthExample = React.createClass({
componentDidMount: function () {
dropboxOauth(config.app_key, (err, access_token) => {
if (err) { console.log(err) }
this.setState({ access_token: access_token })
})
},
onMakeFolderPressed: function () {
fetch(
'https://api.dropbox.com/1/fileops/create_folder',
{
method: 'POST',
headers: {
'Authorization': `Bearer ${this.state && this.state.access_token}`
},
body: `root=auto&path=${Math.random()}`
}
)
},
render: function() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>
Welcome to React Native!
</Text>
<Text style={styles.instructions}>
To get started, edit index.ios.js
</Text>
<Text style={styles.instructions}>
Press Cmd+R to reload,{'\n'}
Cmd+Control+Z for dev menu
</Text>
<Text style={styles.instructions}>
Access Token: {this.state && this.state.access_token}
</Text>
<TouchableHighlight
onPress={this.onMakeFolderPressed.bind(this)}>
<Text>Make Folder</Text>
</TouchableHighlight>
</View>
);
}
});
var styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
}
});
AppRegistry.registerComponent('OauthExample', () => OauthExample);