Skip to content

Commit 063a3b3

Browse files
committed
Merge pull request #60 from ckyang74/master
Better error handling; updated comments; updated example
2 parents 7777fd8 + 3aac257 commit 063a3b3

5 files changed

Lines changed: 201 additions & 130 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
packages/
2+
build/
23
pubspec.lock
34
out
45
credentials.json

example/oauth_example.dart

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,35 @@ final logoutButton = querySelector("#logout");
99
final outputDiv = querySelector("#output");
1010
final DivElement loginWrapper = querySelector("#login_wrapper");
1111
final SelectElement approvalPromptInput = querySelector("#approval_prompt");
12+
final SelectElement immediateInput = querySelector("#immediate");
13+
final SelectElement onlyLoadTokenInput = querySelector("#onlyLoadToken");
1214

1315
void main() {
1416
// use your own Client ID from the API Console here
1517
final auth = new GoogleOAuth2(
1618
"796343192238.apps.googleusercontent.com",
1719
["https://www.googleapis.com/auth/books"]);
1820

21+
outputDiv.innerHtml = "";
22+
1923
loginButton.onClick.listen((e) {
24+
outputDiv.innerHtml = "Loading...";
2025
loginButton.disabled = true;
2126
String approvalPrompt = approvalPromptInput.value;
2227
if (approvalPrompt.isEmpty) {
2328
approvalPrompt = null;
2429
}
2530
auth.approval_prompt = approvalPrompt;
26-
auth.login()
31+
bool isImmediate = (immediateInput.value == "1");
32+
bool onlyLoadToken = (onlyLoadTokenInput.value == "1");
33+
auth.login(immediate: isImmediate, onlyLoadToken: onlyLoadToken)
2734
.then(_oauthReady)
2835
.whenComplete(() {
2936
loginButton.disabled = false;
37+
})
38+
.catchError((e) {
39+
outputDiv.innerHtml = e.toString();
40+
print("$e");
3041
});
3142
});
3243

@@ -50,7 +61,10 @@ Future _oauthReady(Token token) {
5061
.then((HttpRequest request) {
5162
if (request.status == 200) {
5263
var data = JSON.decode(request.responseText);
53-
outputDiv.innerHtml = "Book info:\n${data['volumeInfo']['title']}";
64+
outputDiv.innerHtml = """
65+
<p>Book title: ${data['volumeInfo']['title']}</p>
66+
<p>Description:<br> ${data['volumeInfo']['description']}</p>
67+
""";
5468
} else {
5569
outputDiv.innerHtml = "Error ${request.status}: ${request.statusText}";
5670
}

example/oauth_example.html

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,31 @@
88
<body>
99
<h1>OAuth Example</h1>
1010

11+
<p>After logging in, this app will call Google Books API to get book info</p>
1112
<div id="login_wrapper">
1213
<p>approval_prompt
1314
<select id="approval_prompt">
1415
<option value="auto">auto</option>
15-
<option value="force">force</option>
16-
<option value="">(null)</option>
16+
<option value="force" selected>force</option>
17+
</select>
18+
</p>
19+
<p>immediate
20+
<select id="immediate">
21+
<option value="1">true</option>
22+
<option value="0" selected>false</option>
23+
</select>
24+
</p>
25+
<p>onlyLoadToken
26+
<select id="onlyLoadToken">
27+
<option value="1">true</option>
28+
<option value="0" selected>false</option>
1729
</select>
1830
</p>
1931
<button id="login">Login</button>
2032
</div>
2133
<button id="logout" style="display:none;">Logout</button>
2234

23-
<div id="output"></div>
35+
<div id="output" style="margin:15px 5px 0 5px;"></div>
2436

2537
<script type="application/dart" src="oauth_example.dart"></script>
2638
<script src="packages/browser/dart.js"></script>

0 commit comments

Comments
 (0)