.NET Google API access token failing with no refresh token specified - vb.net

I am trying to set up a class that can wrap around the .NET Google API so that I can use an Access Token that I have previously obtained to access a user's Google Drive. As of right now, I am just trying to get it to work so that I do not require a Refresh Token (more on that in a second). The ultimate goal is for somebody to go through a web page I have set up to authenticate where I obtain both an Access Token and a Refresh Token by directly calling to the Google Rest API (which I store in a database). They can then request to upload/download files onto their Drive on a different page which will first obtain the appropriate information from the database and then use the .NET Google API Library when accessing Drive.
However, when I attempt to access their Drive I get the the following error:
The access token has expired and could not be refreshed. Errors: refresh error, refresh error, refresh error
I know that the Access Token is valid because I obtain it only seconds earlier during my testing. Here is my code for setting up the Drive Service:
' NOTE: Code altered for brevity
Public Sub Initialize(accessToken As String)
' Set up the client secret information based on the default constants
Dim clientSecrets As New ClientSecrets()
clientSecrets.ClientId = DEFAULT_CLIENT_ID
clientSecrets.ClientSecret = DEFAULT_CLIENT_SECRET
' Set up a token based on the token data we got
' NOTE: Is it OK to leave some strings as NULL?
Dim token As New Responses.TokenResponse()
token.AccessToken = accessToken
token.RefreshToken = ""
token.TokenType = "Bearer"
token.IssuedUtc = DateTime.Now
token.ExpiresInSeconds = 3600
token.Scope = "drive"
token.IdToken = ""
' Set up a flow for the user credential
Dim init As New GoogleAuthorizationCodeFlow.Initializer()
init.ClientSecrets = clientSecrets
init.Scopes = New String() {DriveService.Scope.Drive}
init.Clock = Google.Apis.Util.SystemClock.Default
' Set up everything else and initialize the service
Dim baseInit As New BaseClientService.Initializer()
baseInit.HttpClientInitializer = New UserCredential(New GoogleAuthorizationCodeFlow(init), "user", token)
baseInit.ApplicationName = APP_NAME
_service = New DriveService(baseInit)
End Sub
Shortly after that, I then use the following code to request a folder so I can check to see if it exists or not.
Private Function GetDriveFolder(folderPath As String, ByRef folderIds As String(), Optional createMissingFolders As Boolean = False, Optional parentFolderId As String = "root") As Data.File
Dim creatingFolderPath As Boolean = False
Dim currentFolder As Data.File = Nothing
Dim folderPathSplit As String() = folderPath.Replace("/", "\").Trim("\").Split("\")
Dim folderIdList As New List(Of String)
folderIds = {}
' Loop through each folder in the path and seek each out until we reach the end
For x As Integer = 0 To folderPathSplit.Length - 1
Dim result As FileList = Nothing
If Not creatingFolderPath Then
' Build a list request which we will use to seek out the next folder
Dim request As FilesResource.ListRequest = _service.Files.List()
request.Q = "mimeType='application/vnd.google-apps.folder' and name='" & folderPathSplit(x) & "'"
If currentFolder Is Nothing Then
request.Q &= " and '" & EscapeDriveValue(parentFolderId) & "' in parents"
Else
request.Q &= " and '" & EscapeDriveValue(currentFolder.Id) & "' in parents"
End If
request.Spaces = "drive"
request.Fields = "files(id, name)"
' Execute the search, we should only get a single item back
' NOTE: Error thrown on this request
result = request.Execute()
End If
' So on.....
So, I'm just trying to get it to work with only the Access Token for the time being because if it ends up getting refreshed I'll need to know so that I can update my database. However, if I do include the Refresh Token I get the following error:
Error:"unauthorized_client", Description:"Unauthorized", Uri:""
I'm guessing this has something to do with the way I have configured my application through the Dev Console but if I authenticate through the Google API Library by having it launch a browser to get my credentials everything works fine. So, I'm really not sure where to go from here as I haven't found anybody having similar problems and the guides don't cover specifying your own Access Token.
Also, as a quick note this is the URL I am using when having the user authenticate:
String.Format("https://accounts.google.com/o/oauth2/v2/auth?client_id={0}&state={1}&redirect_uri={2}&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive&access_type=offline&include_granted_scopes=true&prompt=select_account%20consent&response_type=code", GOOGLEAPI_CLIENTID, validateState, redirectUri)
Thanks for the help!

If you have an access-token then the simplest way to create a google credential is to use the GoogleCredential.FromAccessToken() method passing in your access token.
This returns you a GoogleCredential instance which you can use to set the HttpClientInitializer property when building the DriveService.
If you then still get an error when accessing the drive service, then it's likely there's something incorrect in how you are asking for the access-token.

Related

How to get the FolderID from Google Drive folders using Chilkat DLL

Im using the ChilKat to develop a tool using VB.NET, that performs a single file upload to my google drive account.
Im able to get the folderID within a root folder, but im struggling to get the folderID if there is a path of folders.
At this moment the argument FolderPath is not being used (i will, when i uncover how to properly get the FolderID). For now, i can get the "Nova" folder id, but none of the other folders within the following tree:
Is there an easier way to get the folderID from GoogleDrive?
I would also like to create the path of folders on Google Drive, in case they dont exist.
I never worked with JSON or HTTP requests, so im kind of lost here.
Any help would be mostly appreciated!
Thanks in advance!
Private Function FolderID(ByVal FolderPath As String) As String
Dim rest As New Chilkat.Rest
' Connect using TLS.
Dim success As Boolean = rest.Connect("www.googleapis.com", 443, True, True)
' Provide the authentication credentials (i.e. the access token)
Dim gAuth As New Chilkat.AuthGoogle
gAuth.AccessToken = M_AccessToken
rest.SetAuthGoogle(gAuth)
Dim json As New Chilkat.JsonObject
json.EmitCompact = False
' Get the folder Testes folder that is in the Google Drive root.
rest.AddQueryParam("q", "'root' in parents and name='Testes'")
Dim jsonResponse As String = rest.FullRequestNoBody("GET", "/drive/v3/files")
If Not rest.LastMethodSuccess Then
Return rest.LastErrorText
Exit Function
End If
json.Load(jsonResponse)
rest.ClearAllQueryParams()
' Now that we know the ID for the Testes directory, get the id for the folder Nova having Testes as the parent.
Dim sbQuery As New Chilkat.StringBuilder
sbQuery.Append("name = 'nova' and '")
sbQuery.Append(json.StringOf("files[0].id"))
sbQuery.Append("' in parents")
rest.AddQueryParamSb("q", sbQuery)
jsonResponse = rest.FullRequestNoBody("GET", "/drive/v3/files")
If Not rest.LastMethodSuccess Then
Return (rest.LastErrorText)
Exit Function
End If
json.Load(jsonResponse)
Return json.StringOf("files[0].id")
End Function
I managed a way by performing iterative requests.
Don't know if this is the correct way, but it works...
Here's the code, now using the FolderPath with format /folder1/folder2/folderN
Private Function GetFolderID(ByVal FolderPath As String) As String
Dim Rest As New Chilkat.Rest
' Connect to Google APIs server
Dim Connected As Boolean = Rest.Connect("www.googleapis.com", 443, True, True)
If Not Connected Then
Return "Error attempting to connect: " & Rest.ConnectFailReason
Exit Function
End If
' Provide the Access token
Dim GAuth As New Chilkat.AuthGoogle
GAuth.AccessToken = M_AccessToken
Rest.SetAuthGoogle(GAuth)
' Instance to JSON object
Dim JSON As New Chilkat.JsonObject
JSON.EmitCompact = False
' Parse the provided path and split to array
Dim ParseFolder As String = Strings.Right(FolderPath, Len(FolderPath) - 1)
Dim Folders As String() = Split(ParseFolder, "/")
' Get the root folder that is in the Google Drive folders structure
Rest.AddQueryParam("q", "'root' in parents and name='" & Folders(0) & "'")
Dim Response As String = Rest.FullRequestNoBody("GET", "/drive/v3/files")
If Not Rest.LastMethodSuccess Then
Return Rest.LastErrorText
Exit Function
End If
JSON.Load(Response)
'Iterate on the folders to get the last folder's id
Rest.ClearAllQueryParams()
For i = 1 To Folders.Length - 1
Dim sbQuery As New Chilkat.StringBuilder
sbQuery.Append("name = '" & Folders(i) & "' and '")
sbQuery.Append(JSON.StringOf("files[0].id"))
sbQuery.Append("' in parents")
Rest.AddQueryParamSb("q", sbQuery)
Response = Rest.FullRequestNoBody("GET", "/drive/v3/files")
If Not Rest.LastMethodSuccess Then
Return Rest.LastErrorText
Exit Function
End If
JSON.Load(Response)
Next
' Get the folder id
Return JSON.StringOf("files[0].id")
End Function

Microsoft Teams API returning Unauthorized

So I am working on setting up a new (private) bot for Microsoft Teams that should be able to post messages in a channel on-demand. I already have a bot coded for Google Hangouts Chat, but Microsoft Teams is giving me a really hard time.
I've been searching for over 10 hours now all over the web, and I am very confused.
Right now, all I want to do is post cards to a Microsoft Teams Channel. So I created the connectorclient, I used the baseuri provided when the bot joined the channel (Starts with smba.trafficmanager.net) with my MSAppID and MSAppPassword. Then, I fill in as much information as I can (Maybe too much?) and I submit the information using the connector's .conversations.createconversation.
Namespaces used: Microsoft.bot.connector, Microsoft.bot.connector.teams.models
Here's the code:
Dim Connector As New ConnectorClient(New Uri("https://smba.trafficmanager.net/amer/"), "MSAPPID", "MSAPPPASSWORD")
Dim conversation As New ConversationParameters
Dim activity2 = Activity.CreateMessageActivity
Dim bot As New ChannelAccount
bot.Id = "BOTID"
bot.Name = "EDD Bot Test"
conversation.Bot = bot
Dim chaninfo As New ChannelInfo
chaninfo.Id = "CHANID"
chaninfo.Name = "General"
Dim teaminfo As New TeamInfo
teaminfo.Id = "TEAMID"
teaminfo.Name = "EDD"
activity2.Text = "Test"
activity2.ServiceUrl = "https://smba.trafficmanager.net/amer/"
activity2.Type = ActivityTypes.Message
activity2.From = bot
activity2.ChannelId = "msteams"
Dim tenantdata As New TenantInfo
tenantdata.Id = "TENANTID"
Dim teamschanneldata As New TeamsChannelData
teamschanneldata.Channel = chaninfo
teamschanneldata.Team = teaminfo
teamschanneldata.Tenant = tenantdata
activity2.ChannelData = teamschanneldata
conversation.Activity = activity2
Response.Write(JsonConvert.SerializeObject(conversation))
Try
Dim reqresp As ConversationResourceResponse = Connector.Conversations.CreateConversation(conversation)
Response.Write("ActivityID: " & reqresp.ActivityId & ", ServiceURL: " & reqresp.ServiceUrl & ", ID: " & reqresp.ServiceUrl)
Catch ex As ErrorResponseException
Response.Write(ex.Response.Content & " " & ex.Response.ReasonPhrase)
End Try
This is what the API returns:
{"message":"Authorization has been denied for this request."} Unauthorized
Keep in mind, I'm not even 100% sure I'm using the right method to create the channel message, I figured it was either CreateConversation or ReplyToActivity.
I gave the app Users.ReadWriteAll permissions too, so am I missing something? That error leads me to think it doesn't have anything to do with the ConversationParameters payload but something to do with authentication.
Problem was solved by using MicrosoftAppCredentials.TrustserviceUrl for both the serviceUrl and the endpoint message.

Visual studio 2008 express crashes when writing method call

I'm using to Google Calendar API v3 and following the samples to create the windows form application I need. I have an issue in the getAuthorization method provided in the sample.
Basically whenever I write down aut.RefreshToken(... Visual studio crashes
Here's my getAuthorization method:
Private Shared Function GetAuthorization(ByVal arg As NativeApplicationClient) As IAuthorizationState
Dim scopes() As String = { _
CalendarService.Scopes.Calendar.GetStringValue, _
CalendarService.Scopes.CalendarReadonly.GetStringValue _
}
' Get the auth URL:
Dim state As IAuthorizationState = New AuthorizationState(scopes)
state.Callback = New Uri(NativeApplicationClient.OutOfBandCallbackUrl)
Dim refreshToken As String = GetRefreshToken()
If refreshToken <> "" Then
state.RefreshToken = refreshToken
'If arg.RefreshToken(state) Then
' SaveRefreshToken(refreshToken)
' Return state
'End If
End If
Dim authUri As Uri = arg.RequestUserAuthorization(state)
' Request authorization from the user (by opening a browser window):
Process.Start(authUri.ToString())
Dim authCode As String = ""
While authCode = ""
authCode = InputBox("Paste the code provided by Google here:", "Verification code")
End While
' Retrieve the access token by using the authorization code:
Dim result = arg.ProcessUserAuthorization(authCode, state)
SaveRefreshToken(refreshToken)
Return result
End Function
As it can be seen, the arg.RefreshToken block has been commented. If I try to uncomment it or writing it again, VS crashes.
I have to point out that I've tried this code on VS Pro 2010 and worked without any problems
Compiling the method in a dll on its own in VS 2010 worked as I expected, however if anyone ever encountered anything similar, I would appreciate any help as this solution is far from ideal.

Set "client_updated_time" when uploading file to OneDrive

I have nearly completed a basic OneDrive interface, and am able to handle creating and writing folders, files, and so forth. The last element to get working is to update a file's date/time stamp to match that of the local (originating) file.
When I drop a file via the browser interface, the file's date/time stamp shows correctly in the view. This is reflected in the "client_updated_time" when I read the file's properties later in my application. Clear enough.
However, I cannot find any way to update this field programmatically from within my application. I am using the following code, to no avail. I have a valid _accessToken value, a valid fileId for the new file, and the call results always indicates success.
The "name" and "updated_time" elements are just in there to see if anything happens, and the file will indeed rename if I mangle the fileName variable a bit. I didn't expect the "updated_time" to updated, but it seems to me that the "client_updated_time" element should work.
Using fiddler, it appears that the browser-based interface (java?) opens a session, sends the file over, and then in the close-session call uses a header entry labelled "X-Last-Modified-ISO8601" to set the file's date-time stamp. However, using the REST interface, I cannot find any examples of this. The documentation for setting file properties mentions renaming only (which works in this code).
Any feedback on how to accomplish setting "client_updated_time" with REST calls would be much appreciated!
Here's the relevant code:
Private _liveURL As String = "https://apis.live.net/v5.0/"
Private Sub AddAuthorizationHeader(hc As HttpClient, authorization As String)
If Len(authorization) > 0 Then
hc.DefaultRequestHeaders.Authorization = New Headers.AuthenticationHeaderValue("Bearer", authorization)
End If
End Sub
Public Function WebPUT(uri As String, contentType As String, authorization As String, data As String) As String
Dim response As String = String.Empty
Try
Dim hc As New HttpClient()
Dim content As New Http.StringContent(data, System.Text.Encoding.UTF8, contentType)
AddAuthorizationHeader(hc, authorization)
Using r = hc.PutAsync(uri, content).Result
response = r.Content.ReadAsStringAsync.Result
End Using
Catch ex As Exception
' fake it
response = "{""error"": {""code"": ""invalid_request"", ""message"": """ + ex.Message + """}}"
End Try
Return response
End Function
Private Function UpdateFileDateTime(fileId As String, fileName As String, fileDt As String) As Boolean
Dim response As Boolean = False
Dim wr As String = WebHelper.WebPUT(_liveURL + fileId, "application/json", _accessToken, "{ ""name"": """ + fileName + """, ""updated_time"": """ + fileDt + """, ""client_updated_time"": """ + fileDt + """ }")
'... parse wr for response
Return response
End Function
Based on the file object reference, that field is read-only via the REST API: http://msdn.microsoft.com/en-us/library/dn631834.aspx

DocuSign SOAP API Update (Correct) Envelope Expiration

I'm using vb.net (4.0) to interact with the DocuSign API. I'm trying to make a process that allows a user to add 30 days to the current expiration date instead of logging into DocuSign.net to correct the envelope. The code seems to work fine (doesn't throw any errors) but the correction doesn't happen on DocuSign's side.
Me.EnvelopeID is the Envelope's ID
DocuService is the namespace of the DocuSign API Service Reference.
Me.AuthorizationString is the Username, Password, Account# and Integrator Key to send as HTTP headers.
Private Sub UpdateExpiration()
'Get envelope details
Dim orig As DocuService.Envelope = ExecuteSoap(Function(client) client.RequestEnvelope(Me.EnvelopeID, False), Me.AuthorizationString)
Dim cor As New DocuService.Correction
cor.EnvelopeID = Me.EnvelopeID
cor.Reminders = orig.Notification.Reminders
cor.Expirations = orig.Notification.Expirations
cor.Expirations.ExpireAfter = (Integer.Parse(orig.Notification.Expirations.ExpireAfter) + 30)
'Execute Correction
Dim cord As DocuService.CorrectionStatus = Me.ExecuteSoap(Function(client) client.CorrectAndResendEnvelope(cor), Me.AuthorizationString)
'If I add a break point on the next line and check the values of cord,
'there is a returned CorrectionStatus object but every property in the object is "Nothing"
Dim check As DocuService.Envelope = ExecuteSoap(Function(client) client.RequestEnvelope(Me.EnvelopeID, False), Me.AuthorizationString)
Console.WriteLine(check.Notification.Expirations.ExpireAfter & " " & (Integer.Parse(orig.Notification.Expirations.ExpireAfter) + 30))
If check.Notification.Expirations.ExpireAfter = (Integer.Parse(orig.Notification.Expirations.ExpireAfter)) Then
'Success :)
MsgBox("success!")
Else
'Failure :(
MsgBox("failure!")
End If
End Sub
Private Function ExecuteSoap(Of TResult)(func As Func(Of DSAPIServiceSoapClient, TResult), authorizationString As String) As TResult
Using client As New DocuService.DSAPIServiceSoapClient(My.Settings.DocusignMode)
Using scope As OperationContextScope = New System.ServiceModel.OperationContextScope(client.InnerChannel)
Dim hp As HttpRequestMessageProperty = New HttpRequestMessageProperty
hp.Headers.Add("X-Docusign-Authentication", authorizationString)
OperationContext.Current.OutgoingMessageProperties(HttpRequestMessageProperty.Name) = hp
Return If(func IsNot Nothing, func(client), Nothing)
End Using
End Using
End Function
We use the same ExecuteSOAP function and AuthorizationString to create & send envelops, and do recipient updates so I know these are correct. I'm not sure whats wrong!
This is most likely 1 of 3 possibilities.
I'm surprised its not error-ing out, but you should not be putting your accountId in the http auth header. See page 19 the SOAP PDF guide:
http://www.docusign.com/sites/default/files/DocuSignAPI_Guide.pdf
There is something else not configured correctly with your SOAP API call. Inspect your raw outgoing request and ensure the xml is what you expect. Post the raw request here if not sure.
A bug with DocuSign. First rule out the other two options and if no dice still post a comment here and I can get a bug logged on DocuSign's side. Also a good test would be to make the correction call through the REST api to see if you can get that to work.