-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Started working with the Google API.
- Loading branch information
Showing
8 changed files
with
150 additions
and
1 deletion.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
# Private API keys file | ||
*APIKeys.cpp | ||
|
||
# Prerequisites | ||
*.d | ||
|
||
|
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#include <emscripten.h> | ||
#include <emscripten/val.h> | ||
#include <Defines.h> | ||
|
||
// This is the function getting called by the Google API once it's loaded. | ||
extern"C" EMSCRIPTEN_KEEPALIVE void GAPI_Init_Client() | ||
{ | ||
EM_ASM({init_gapi_with_key(Emval.toValue($0));}, VAR_TO_JS("API KEY GOES HERE")); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#include <string> | ||
|
||
// !! It's important to NOT save or return keys to javascript and to NEVER use this with function pointers for security reasons! | ||
namespace APIKeys | ||
{ | ||
// The Google API key. | ||
// !! It's important to NOT save or return keys to javascript and to NEVER use this with function pointers for security reasons! | ||
std::string Google(); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
#include "GoogleDrive.h" | ||
#include <emscripten.h> | ||
#include <emscripten/val.h> | ||
#include <Defines.h> | ||
#include "APIKeys.h" | ||
|
||
EM_JS(void, gapi_loaded, (), { | ||
gapi.load('client', _GAPI_Init_Client); | ||
gapi.load('client:picker', initializePicker); | ||
}); | ||
|
||
EM_JS(bool, gapi_ready, (), { | ||
return global_gapi_inited && global_gis_inited; | ||
} | ||
var global_gapi_inited = false; | ||
var global_gis_inited = false; | ||
); | ||
|
||
EM_JS(void, gis_loaded, (), { | ||
global_client_token = google.accounts.oauth2.initTokenClient({ | ||
client_id: CLIENT_ID, | ||
scope: SCOPES, | ||
callback: '', // defined later | ||
}); | ||
global_gis_inited = true; | ||
} | ||
var global_client_token; | ||
); | ||
|
||
extern"C" EMSCRIPTEN_KEEPALIVE void GAPI_Init_Client() | ||
{ | ||
EM_ASM({init_gapi_with_key(Emval.toValue($0));}, VAR_TO_JS(APIKeys::Google())); | ||
} | ||
EM_JS(void, init_gapi_with_key, (emscripten::EM_VAL APIKey), { | ||
gapi.client.init({ | ||
apiKey: Emval.toValue(APIKey), | ||
discoveryDocs: ['https://www.googleapis.com/discovery/v1/apis/drive/v3/rest'] | ||
}).then(()=>{global_gapi_inited = true;}); | ||
}); | ||
|
||
EM_JS(bool, has_gapi_token, (), { | ||
return gapi.client.getToken() !== null; | ||
}); | ||
|
||
EM_JS(void, request_client_token, (), { | ||
if (gapi.client.getToken() === null) { | ||
// Prompt the user to select a Google Account and ask for consent to share their data | ||
// when establishing a new session. | ||
global_client_token.requestAccessToken({prompt: 'consent'}); | ||
} else { | ||
// Skip display of account chooser and consent dialog for an existing session. | ||
global_client_token.requestAccessToken({prompt: ''}); | ||
} | ||
}); | ||
|
||
EM_JS(void, revoke_client_token, (), { | ||
const token = gapi.client.getToken(); | ||
if (token !== null) { | ||
google.accounts.oauth2.revoke(token.access_token); | ||
gapi.client.setToken(''); | ||
} | ||
}); | ||
|
||
EM_JS(void, create_picker, (emscripten::EM_VAL APIKey), | ||
{ | ||
|
||
}) | ||
|
||
bool GoogleDrive::Ready() | ||
{ | ||
return gapi_ready(); | ||
} | ||
|
||
bool GoogleDrive::HasToken() | ||
{ | ||
return has_gapi_token(); | ||
} | ||
|
||
void GoogleDrive::RequestToken() | ||
{ | ||
} | ||
|
||
void GoogleDrive::LogOut() | ||
{ | ||
} | ||
|
||
std::string GoogleDrive::LoadProject() | ||
{ | ||
return std::string(); | ||
} | ||
|
||
std::string GoogleDrive::SaveProject() | ||
{ | ||
return std::string(); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#include <string> | ||
|
||
namespace GoogleDrive | ||
{ | ||
bool Ready(); | ||
bool HasToken(); | ||
void RequestToken(); | ||
void LogOut(); | ||
|
||
std::string LoadProject(); | ||
std::string SaveProject(); | ||
} |
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