Morning,
Hoping someone can help me out.
I’m currently getting the below response when making the call “envelopesApi.CreateEnvelope(accountId, envDef)”:
{"Error calling CreateEnvelope: {""errorCode"":""AUTHORIZATION_INVALID_TOKEN"",""message"":""The access token provided is expired, revoked or malformed. Authentication for System Application failed.""}"}
I’m providing a valid access token to the call, and had previously had this working when using the link https://developers.docusign.com/token-generator/ to generate the Token for me while testing. When moving to automatically generate the token, the token provided is no longer working.
Please see my code below for the whole process, I’m storing the the details in an object to access them, if these details are required please let me know:
Private Sub SendToDocuSign(firstName As String, lastName As String, email As String, body As String, docsignAttachment As String)
Dim baseUri As String = String.Format(oSetting.APITokenURL)
Dim codeAuth As String = oSetting.APIIntegratioKey & ":" & oSetting.APISecrect
Dim codeAuthBytes As Byte() = System.Text.Encoding.UTF8.GetBytes(codeAuth)
Dim codeAuthBase64 As String = Convert.ToBase64String(codeAuthBytes)
Dim r As HttpWebRequest = CType(WebRequest.Create(baseUri), HttpWebRequest)
'If oSetting.APIToken <> "" Then
' r.Headers.Add("Authorization", "Bearer " & codeAuthBase64)
'Else
r.Headers.Add("Authorization", "Basic " & codeAuthBase64)
'End If
r.Headers.Add("Cache-Control", "no-store")
r.Headers.Add("Pragma", "no-cache")
r.ContentType = "application/x-www-form-urlencoded"
Dim obody As String
'If oSetting.APIToken <> "" Then
' obody = "grant_type=refresh_token&refresh_token=" & Web.HttpUtility.UrlEncode(oSetting.APIToken)
'Else
obody = "grant_type=client_credentials"
'End If
Dim strResponse = SendRESTRequest(r, "POST", obody)
Dim oTokenResponse As TokenResponse = JsonConvert.DeserializeObject(Of TokenResponse)(strResponse)
oSetting.APIToken = oTokenResponse.AccessToken
oSetting.Update()
Dim apiClient = New ApiClient(oSetting.APIURL)
' Set OAuth token
Dim accessToken = oSetting.APIToken ' Use your OAuth2 access token
apiClient.Configuration.DefaultHeader.Add("Authorization", "Bearer " & oSetting.APIToken)
' Your account ID (get this from your DocuSign dashboard or API response)
Dim accountId As String = oSetting.APIAccount
' Create Envelope Definition
Dim envDef As EnvelopeDefinition = New EnvelopeDefinition()
envDef.EmailSubject = If(oSetting.EmailSubject <> "", oSetting.EmailSubject, "Please Sign this Document")
' Add a document to the envelope
If docsignAttachment <> "" Then
Dim docBytes As Byte() = File.ReadAllBytes(docsignAttachment)
Dim document As Document = New Document()
document.DocumentBase64 = Convert.ToBase64String(docBytes)
document.Name = "Document Confirmation"
document.DocumentId = "1"
envDef.Documents = New List(Of Document)() From {document}
End If
' Add a recipient to the envelope
Dim signer As Signer = New Signer()
signer.Email = email
signer.Name = $"{firstName} {lastName}"
signer.RecipientId = "1"
' Create a signHere tab for the signer
Dim signHere As SignHere = New SignHere()
signHere.DocumentId = "1"
signHere.PageNumber = "1"
signHere.RecipientId = "1"
signHere.XPosition = "100"
signHere.YPosition = "150"
' Add tabs to the signer
Dim tabs As Tabs = New Tabs()
tabs.SignHereTabs = New List(Of SignHere)() From {signHere}
signer.Tabs = tabs
' Add the signer to the envelope
envDef.Recipients = New Recipients()
envDef.Recipients.Signers = New List(Of Signer)() From {signer}
' Set the envelope status to "sent" to send it immediately
envDef.Status = "sent"
' Use the EnvelopesApi to send the envelope
Dim envelopesApi As New EnvelopesApi(apiClient)
Dim envelopeSummary As EnvelopeSummary = envelopesApi.CreateEnvelope(accountId, envDef)
End Sub
Public Shared Function SendRESTRequest(r As Net.HttpWebRequest, strMethod As String, strContent As String, Optional oStream As Boolean = False) As String
With r
.Timeout = 300000
.Method = strMethod
If strContent <> "" Then
.ContentLength = strContent.Length
End If
End With
If strContent <> "" Then
Dim d As Byte() = System.Text.Encoding.UTF8.GetBytes(strContent)
Using newStream As Stream = r.GetRequestStream()
newStream.Write(d, 0, strContent.Length)
newStream.Flush()
newStream.Close()
End Using
End If
Dim Response As HttpWebResponse = Nothing
Try
Response = CType(r.GetResponse(), HttpWebResponse)
Catch ex2 As WebException
Response = ex2.Response
Catch ex As Exception
Dim strStreamResponse2 As String
Using stmReader As New StreamReader(Response.GetResponseStream, True)
strStreamResponse2 = stmReader.ReadToEnd()
stmReader.Close()
End Using
Return strStreamResponse2
End Try
If oStream = True Then
Dim strStreamResponse2 As String
Using stmReader As New StreamReader(Response.GetResponseStream, True)
strStreamResponse2 = stmReader.ReadToEnd()
stmReader.Close()
End Using
Return strStreamResponse2
End If
If Response.ContentLength > 0 Then
Dim bytRes(CInt(Response.ContentLength) - 1) As Byte
Dim StreamResponse As Stream = Response.GetResponseStream()
Using stm = Response.GetResponseStream()
stm.Read(bytRes, 0, CInt(Response.ContentLength))
stm.Flush()
stm.Close()
End Using
Return System.Text.Encoding.UTF8.GetString(bytRes)
Else
Dim strStreamResponse2 As String
Using stmReader As New StreamReader(Response.GetResponseStream, True)
strStreamResponse2 = stmReader.ReadToEnd()
stmReader.Close()
End Using
If strStreamResponse2 <> "" Then
Return strStreamResponse2
Else
Throw New Exception("unable to get response")
End If
End If
End Function
Please let me know if further details are required.