access Google spreadsheet with oAuth2
I am trying to get a solution that I can have a process that signs into a Google spreadsheet and grabs some values without using regular Google username/passsword and instead use oAuth2. The below seems like it works but I have to do a manual step of taking the url in sAuthURL and manually enter into a browser and click 'Allow access' button and then it redirects to a url like so:
.../googlespreadsheettest.aspx?code=4/O*****************************************
Is it possible to do without the manual step?
Thanks.
Code:
Imports Google.GData.Spreadsheets
Imports Google.Spreadsheets
Imports Google.GData.Client
...
Protected Sub form1_Load(sender As Object, e As System.EventArgs) Handles form1.Load
'OBJECTIVE: Log into a Google spreadsheet and grab a worksheets data using OAuth2
'BELOW FROM: https://developers.google.com/google-apps/spreadsheets/
'OAuth 2.0 info
Dim sCLIENT_ID = "###########.apps.googleusercontent.com"
Dim sCLIENT_SECRET = "E******************G"
Dim sSCOPE = "https://spreadsheets.google.com/feeds https://docs.google.com/feeds"
Dim sREDIRECT_URI = "http://www.domain.com/googlespreadsheettest.aspx"
'OAuth Object
Dim oaParams As OAuth2Parameters = New OAuth2Parameters
oaParams.ClientId = sCLIENT_ID
oaParams.ClientSecret = sCLIENT_SECRET
oaParams.RedirectUri = sREDIRECT_URI
'Get authorized url
oaParams.Scope = sSCOPE
Dim sAuthURL as String = OAuthUtil.CreateOAuth2AuthorizationUrl(oaParams)
'Response.Write(sAuthURL)
'Response.Write("<br/> Please visit the URL above to authorize your oauth request token.")
'Auto post sAuthURL to get token
Dim wp As WebPost = New WebPost
wp.URL = sAuthURL
Dim sRetHtml As String = wp.Request(String.Empty, True)
Response.Write("<br />" & sRetHtml)
''sRetHtml is returning a page to sign into google. If I output sAuthURL and take it and manually enter into browser and allow access it seems to work.
'oaParams.AccessCode = ""
''Get Access Token
'OAuthUtil.GetAccessToken(oaParams)
'Dim sToken As String = oaParams.AccessToken
'Response.Write("OAuth Access Token: " & sToken)
''make oauth request
'Dim requestFactory As GOAuth2RequestFactory = New GOAuth2RequestFactory(vbNull, "MyAppNameHere", oaParams)
'Dim service As SpreadsheetsService = New SpreadsheetsService("MyAppNameHere")
'service.RequestFactory = requestFactory
End Sub