Problem with Oauth 2.0 in VB .NET

2,812 views
Skip to first unread message

Neil

unread,
Feb 17, 2012, 10:10:37 AM2/17/12
to google-api-dotnet-client
Hi, I'm trying to implement Oauth 2.0 based on the documentation I've
found for C#, but it's not working. The VB code below never gets to
execute the GetAuthorization function.... if I replace :

Dim auth = New OAuth2Authenticator(Of NativeApplicationClient)
(provider, AddressOf GetAuthorization)

With

Dim auth = New OAuth2Authenticator(Of NativeApplicationClient)
(provider, GetAuthorization(provider))

I get the browser and the success code, but when I try to create the
Calendar Service I get an error:

"Unable to cast object of type
'DotNetOpenAuth.OAuth2.AuthorizationState' to type
'System.Func`2[Google.Apis.Authentication.OAuth2.DotNetOpenAuth.NativeApplicationClient,DotNetOpenAuth.OAuth2.IAuthorizationState]'."

This should be really simple, but thanks to errors in most of the
examples I found (and no VB examples at all) I've spent a whole day
just trying to get this working :-( Can anyone help? And please don't
just suggest I use C# - On StackOverflow there's people with the same
problems with that too.

Thanks

**********

Imports System.Diagnostics
Imports DotNetOpenAuth.OAuth2
Imports Google.Apis.Authentication
Imports Google.Apis.Authentication.OAuth2
Imports Google.Apis.Authentication.OAuth2.DotNetOpenAuth
Imports Google.Apis.Calendar.v3
Imports Google.Apis.Calendar.v3.Data
Imports Google.Apis.Util


Public Shared Sub Main(args As String())

Dim provider = New
NativeApplicationClient(GoogleAuthenticationServer.Description)
provider.ClientIdentifier = "<client id>"
provider.ClientSecret = "<client secret>"
Dim auth = New OAuth2Authenticator(Of NativeApplicationClient)
(provider, AddressOf GetAuthorization)

Dim service = New CalendarService(auth)

'Do stuff with the calendar service...

End Sub


Private Shared Function GetAuthorization(arg As
NativeApplicationClient) As IAuthorizationState

Dim scopes As New System.Collections.Generic.List(Of
String)

scopes.Add(CalendarService.Scopes.Calendar.GetStringValue())

Dim state As IAuthorizationState = New AuthorizationState(scopes)
state.Callback = New
Uri(NativeApplicationClient.OutOfBandCallbackUrl)
Dim authUri As Uri = arg.RequestUserAuthorization(state)

Process.Start(authUri.ToString())

' Open a modal dialogue for user to paste the authorization code
from Browser = authCode

Return arg.ProcessUserAuthorization(authCode, state)

End Function


Eyal Peled

unread,
Dec 21, 2012, 6:19:55 PM12/21/12
to google-api-d...@googlegroups.com
Hi,

In the last days I have been working on an OAuth2 sample in VB.NET (Hopefully it will be available shortly).
Meantime, I ran your sample and it worked.

I didn't understand why you want to change the following code:
   Dim auth = New OAuth2Authenticator(Of NativeApplicationClient) (provider, AddressOf GetAuthorization) 
That's the right way to use the GetAuthorization function.

Anyway the following code, runs on my machine (Windows, with the latest library's code):

Imports DotNetOpenAuth.OAuth2

Imports Google.Apis.Authentication.OAuth2
Imports Google.Apis.Authentication.OAuth2.DotNetOpenAuth
Imports Google.Apis.Calendar.v3
Imports Google.Apis.Calendar.v3.Data
Imports Google.Apis.Util

Module Program
 Public Sub Main(args As String())
        Dim provider = New NativeApplicationClient(GoogleAuthenticationServer.Description)
        provider.ClientIdentifier = <client id>
        provider.ClientSecret = <client secret>
        Dim auth = New OAuth2Authenticator(Of NativeApplicationClient)(provider, AddressOf GetAuthorization)

        Dim service = New CalendarService(auth)
        Dim first = service.CalendarList.List.Fetch().Items().First()

        Console.WriteLine(first.Summary)
        Console.ReadLine()
    End Sub

    Private Function GetAuthorization(arg As NativeApplicationClient) As IAuthorizationState

        Dim scopes As New System.Collections.Generic.List(Of String)

        scopes.Add(CalendarService.Scopes.Calendar.GetStringValue())

        Dim state As IAuthorizationState = New AuthorizationState(scopes)
        state.Callback = New Uri(NativeApplicationClient.OutOfBandCallbackUrl)
        Dim authUri As Uri = arg.RequestUserAuthorization(state)

        Process.Start(authUri.ToString())

        ' Open a modal dialogue for user to paste the authorization code from Browser = authCode 

        Dim authCode As String = Console.ReadLine()

        Return arg.ProcessUserAuthorization(authCode, state)

    End Function
End Module

If you still have problems - you can send me your full project with more details on your environment:
- Mono\Windows
- Version of the dotnet library
- Version of the calendar API

and I'll check it.

Thanks!
Eyal


On Wed, Dec 19, 2012 at 4:56 PM, Nick Lee <nl...@yesco.com> wrote:
Same issue here
Reply all
Reply to author
Forward
0 new messages