Page 1 of 1

How to implement oauth for google login from CEF?

PostPosted: Tue Mar 17, 2020 9:04 am
by rjxray
I really need to be able to do this now.
There are guidelines at https://developers.google.com/identity/ ... native-app
Has anyone made any progress at all with this?

Re: How to implement oauth for google login from CEF?

PostPosted: Tue Mar 17, 2020 10:24 am
by salvadordf
We have a demo in the CEF4Delphi project called "OAuth2Tester" that uses CEF to get the OAuth2 access token.

Then you can use the token in the requests to the Google REST APIs but I couldn't find a way to identify the users in regular browser requests. Adding an "Authorization" HTTP header didn't work for normal browsing.

Re: How to implement oauth for google login from CEF?

PostPosted: Tue Mar 17, 2020 12:05 pm
by rjxray
We have a demo in the CEF4Delphi project

Is the code available to help me get started?

Re: How to implement oauth for google login from CEF?

PostPosted: Tue Mar 17, 2020 12:57 pm
by salvadordf
Yes but it uses Pascal, not C/C++, and it uses the CEF4Delphi components.

The Delphi version of that demo is here :
https://github.com/salvadordf/CEF4Delph ... uth2Tester

And the Lazarus/FPC version is here :
https://github.com/salvadordf/CEF4Delph ... uth2Tester

Read the code comments in the *.pas files

That demo uses this unit that may interest you :
https://github.com/salvadordf/CEF4Delph ... Helper.pas

Re: How to implement oauth for google login from CEF?

PostPosted: Wed Mar 18, 2020 4:18 am
by rjxray
Thanks, I'll have a good look.

Re: How to implement oauth for google login from CEF?

PostPosted: Thu Apr 30, 2020 6:01 am
by rjxray
So thanks to @salvadordf and https://github.com/googlesamples/oauth- ... r-windows/ (C#), I managed to get OAuth2 credentials with C++/CEF/OpenFrameworks/Visual Studio/Windows 10.
See attached rather rough and ready code which could easily be adapted to any windows cef app.
I haven't yet tried exchanging the refresh token but it should be straightforward now.

Instead, I've tried using the access token with a browser but like @salvadordf I'm unable to make it work.
Here's how I tried:

Code: Select all
void MyBrowser::authload(string url, string token) {
   CefRefPtr<CefFrame> frame = browser_->GetMainFrame();
   CefRefPtr<CefRequest> request = CefRequest::Create();
   request->SetMethod("GET");
   request->SetURL(url);
   CefRequest::HeaderMap headerMap;
   headerMap.insert(std::make_pair("Authorization", "Bearer%20" + token));
   request->SetHeaderMap(headerMap);
   frame->LoadRequest(request);
}

Using a url which is a shareable link to a presentation on my company's google drive and an access token resulting from an OAuth2 login to my company account I still get asked for user and password.
I've tried several different variants and nothing works.

After a lot of research I'm coming to the conclusion that I can only use the token with direct requests to google API's .
Am I right, or can anyone suggest another way?

I guess that using the google drive API I could download the presentation and open it locally with a CEF browser but I haven't tried going down that rabbit hole yet.
Particularly as to make it work in the real world we would have to submit my app to google for review at a cost of at least $15000 and I have a feeling they may reject this usage.
See https://support.google.com/cloud/answer/9110914