Google Drive

0
0

Hello,

I have a problem to LogIn into Google Drive Account to scan the Files in it.
In my desktop solution it works, but if I try to Login on the Webserver it doesnt.

The user of my wisej-app should be able to give permissions for his Drive-Account so i can read the files in my wisej-app.

What i already have found, is that Google Drive logon on webservers and desktop is different.

On webservers it works only with a “redirect_uri” which will be called back (with a token as parameter) when the user has given his permission.

Please can you support me on this.

 

 

  • You must to post comments
0
0

Hello Luca,

many thanks for your help. Now it works. You rescued me.

 

  • You must to post comments
0
0

OAuth2 authentication works by navigating to the authentication server (authentication url) passing a redirect url (which is your callback basically). The authentication server authenticates and navigates back your web application’s redirect url passing back a code, then the application makes one more requests using the code and receives a token.

SPA apps may do this in a popup window, but it requires the authentication flow to be executed in javascript. Wisej supports both ways. They are both a bit confusing and the samples from microsoft (onedrive) and google are fragmented and not always working.

If you implemented the Web approach (not SPA) you have to handle the redirection with the code. I have attached a sample generics GoogleApiHelper class that works for most Google services, usage:

 

 var service = await GoogleApiHelper.GetServiceAsync<DriveService>(UserId, "Application Name", "http://localhost:64191/");
 var listRequest = service.Files.List();
 listRequest.PageSize = 100;
 listRequest.Fields = "nextPageToken, files(id, name)";

 // List files.
 var files = listRequest.Execute().Files;

Run in an async method, like

private async void button1_Click(object sender, EventArgs e)

It expects a file “client_secrets.json” which is the authorization credentials downloaded from Google developer console. I followed Google web authorization examples and code in github.

You will have to adapt the code to your needs. The solution key is in the Application_ApplicationRefresh handler and the async task callback. The sample also shows you how to make your authentication method async.

HTH

 

Attachment
  • You must to post comments
0
0

Hello Luca,

many thanks for your fast answer.
I am so happy about wisej framework, because it is really the best tool to convert my windows-application to a cloud-app.

And it works so good, that it is not really necessary to be a ASP-NET developer.
But now this is my problem.

In the attachment you can find a sample to connect google drive by different ways.
It authenficates to google, a google window lets select the private account and gives the privileges to read the data in the private drive-account and if this all was successfull the sample shows the filenames of the root-path in drive.

Desktop-connection is working and is no problem,
but after publishing my wisej web application to the webserver) google needs to authenficate by a different way with postback and redirect-uri.
And this is something i didnt need (thanks wisej) until now.

It would be great if you can support me with this, because i spend already a lot of time.

Many thanks in advance

Rupert

 

 

 

 

 

 

  • You must to post comments
0
0

Works the same as it would work with an aspnet application. If you have a downloadable and runnable sample that shows the issue we can try to help. Attach it as a zip. It has to be a compilable visual studio solution.

  • You must to post comments
0
0

Hi Levie,

first i will explain you what i am doing and what intention i have:

Using wisej i developed an accounting system which is used by a lot of users.
Now users should be able to digitalize their receipts.
I thought the simpliest way is, that for digtalizing of receipts users should use their private smartphones and then using the built in function ‘Scanning’ in Google Drive and store the receipts as pfd-files in their own google drive.

So far so good.

But now my central web-app has to let each user access their own google drive and they should be able to store a link to each pdf-file.

Can it be possible that no other developer needed something like this before?
I really searched a lot for some samples in the internet and found nothing.

In console.cloud.google.com you can export 2 types of json files:
One for access via Desktop-Application and
one for access via Web-Server which uses a redirect-url.

Access with the json file for Desktop-Apps is no problem and i am able to access Google Drive on my developer-PC.
But the same app does not work when i publish it to the webserver.
So i read in Google documentation that i have to use the json for Webservers and to change some Google-Api functions to

Google.Apis.Auth.OAuth2.Web.AuthorizationCodeWebApp and to do a request.

But i dont know how to do it  in wisej?

In my added sample code you can find this functions (unconverted as they are) with “System.Web.HttpContext.Current” namespace (how they cannot work)

Please can you help me how to do this?

Many thanks in advance.

Rupert

Because the upload of my samplecode.vb was forbidden, i insert it here:

Public Class GDriveClass
Public Service As DriveService = Nothing

Public BasePath As String = “”

Public OAuth_File_Web As String = “client-secrets-web.json”

Public ApplicationName As String = “AppName”
Public UserId As String = “UserId”
Public Scopes As String() = {DriveService.Scope.Drive}

Private Function Authenticate() As IConfigurableHttpClientInitializer
Dim clientSecrets As ClientSecrets
Dim usercredential As UserCredential
Dim googlecredential As GoogleCredential
Dim oauthfile As FileStream
Dim oauthFilePath As String
Dim token As TokenResponse
Dim flow As GoogleAuthorizationCodeFlow
Dim flowinit As Object

Try

‘Path for json-Files
If BasePath = “” Then
ErrL.Add(“TokenFileName for Json-File is not set”)
Return Nothing
End If

‘ full path and filename of json credential file for web-authorization for google drive
oauthFilePath = IIf(OAuth_File_Web <> “”, Path.Combine(BasePath, OAuth_File_Web), “”)
If oauthFilePath <> “” Then
If System.IO.File.Exists(oauthFilePath) Then
Debug.Print(“Loading Web OAuth2 credentials from ” + oauthFilePath)
oauthfile = System.IO.File.Open(oauthFilePath, FileMode.Open, FileAccess.Read)
flowinit = New GoogleAuthorizationCodeFlow.Initializer() With {
.DataStore = New FileDataStore(BasePath),
.ClientSecretsStream = oauthfile,
.Scopes = Scopes
}
flow = New GoogleAuthorizationCodeFlow(flowinit)

‘until here it works
‘but the next statements i use System.Web.HttpContext.Current which cannot work in my wisej-serverapp
‘but i dont know how do it
Dim Uri As Object = System.Web.HttpContext.Current.Request.Url.ToString()
Dim code As Object = System.Web.HttpContext.Current.Request(“code”)

If code IsNot Nothing Then
token = flow.ExchangeCodeForTokenAsync(UserId, code, Uri.substring(0, Uri.indexof(“?”)), CancellationToken.None).Result
Dim oauthState As Object = Web.AuthWebUtility.ExtracRedirectFromState(flow.DataStore, UserId, System.Web.HttpContext.Current.Request(“State”)).Result
System.Web.HttpContext.Current.Response.Redirect(oauthState)
Else
Dim result As Object = New Web.AuthorizationCodeWebApp(flow, uri, uri).AuthorizeAsync(UserId, CancellationToken.None).Result
If result.RedirectUri IsNot Nothing Then
System.Web.HttpContext.Current.Response.Redirect(result.RedirectUri)
Else
Return result.Credential
End If
End If
End If
End If

Debug.Print(“Could not find authentication credentials”)
Return Nothing
Catch ex As Exception
ErrMgr.Handler(ex)
Finally
CloseObject(oauthfile)
CloseObject(usercredential)
CloseObject(googlecredential)
CloseObject(flowinit)
CloseObject(flow)
End Try
End Function
End Class

 

 

  • You must to post comments
0
0

Hi Rupert,
You can read the token result from the redirect using Application.QueryString[“tokenName”].

If you want to retain the session you could implement IWisejHandler within your Page or Form class which allows you to generate a postback.wx url you can use to process the redirect.
https://docs.wisej.com/api/wisej.core/interfaces/wisej.core.iwisejhandler
You can get the postback url using this.GetServiceUrl().

Is this what you’re trying to achieve? If not, can you please elaborate on what you’re having an issue with and attach a small sample?

Thanks,
Levie

  • You must to post comments
Showing 6 results
Your Answer

Please first to submit.