Nearing a month working on this with little success, any help would be appreciated a lot.
I have to get calendar items from outlook accounts. Managed to get it to work in ASP.NET MVC with C#, ported the code over to VB.NET on ASP.NET WebForms, but with no success.
I can only find code samples and authorization methods that work with MVC, but the application I have to implement it in still uses WebForms (aspx pages)
I've tried both the Microsoft Restful API and the Graph API, but they both only seem to support MVC. Same thing with the authorization methods they require, (Azure AD v1, v2, oauth, sdk)
Any suggestions?
The Graph Client library may not support the webForm(VB.net), but the Restful Graph API should works well. The restful Graph API can be used by almost any language or framework . For VB.NET on ASP.NET WebForms, because it is not recommended/supported by the official now, so many logic we need to handle by ourselves(HttpClient) or use 3rd lib.
Just one vb.net sample for your reference, ignore the WebForms itself, just copy the code to where you need and modify the logic to what you want.
https://www.example-code.com/vbnet/microsoft_graph_oauth2_access_token.asp
Dim oauth2 As New Chilkat.OAuth2
Dim success As Boolean
' This should be the port in the localhost callback URL for your app.
' The callback URL would look like "http://localhost:3017/" if the port number is 3017.
oauth2.ListenPort = 3017
oauth2.AuthorizationEndpoint = "https://login.microsoftonline.com/common/oauth2/v2.0/authorize"
oauth2.TokenEndpoint = "https://login.microsoftonline.com/common/oauth2/v2.0/token"
' Replace these with actual values.
oauth2.ClientId = "MICROSOFT-GRAPH-CLIENT-ID"
' This is your app password:
oauth2.ClientSecret = "MICROSOFT-GRAPH-CLIENT-SECRET"
oauth2.CodeChallenge = False
' Provide a SPACE separated list of scopes.
' See https://developer.microsoft.com/en-us/graph/docs/authorization/permission_scopes
' Important: To get a refresh token in the final response, you have to ask for "offline_access" scope
oauth2.Scope = "openid profile offline_access user.readwrite mail.readwrite mail.send"
' Begin the OAuth2 three-legged flow. This returns a URL that should be loaded in a browser.
Dim url As String = oauth2.StartAuth()
If (oauth2.LastMethodSuccess <> True) Then
Debug.WriteLine(oauth2.LastErrorText)
Exit Sub
End If
' At this point, your application should load the URL in a browser.
' For example,
' in C#: System.Diagnostics.Process.Start(url);
' in Java: Desktop.getDesktop().browse(new URI(url));
' in VBScript: Set wsh=WScript.CreateObject("WScript.Shell")
' wsh.Run url
' in Xojo: ShowURL(url) (see http://docs.xojo.com/index.php/ShowURL)
' The Microsoft account owner would interactively accept or deny the authorization request.
' Add the code to load the url in a web browser here...
' Add the code to load the url in a web browser here...
' Add the code to load the url in a web browser here...
' Now wait for the authorization.
' We'll wait for a max of 30 seconds.
Dim numMsWaited As Integer = 0
While (numMsWaited < 30000) And (oauth2.AuthFlowState < 3)
oauth2.SleepMs(100)
numMsWaited = numMsWaited + 100
End While
' If there was no response from the browser within 30 seconds, then
' the AuthFlowState will be equal to 1 or 2.
' 1: Waiting for Redirect. The OAuth2 background thread is waiting to receive the redirect HTTP request from the browser.
' 2: Waiting for Final Response. The OAuth2 background thread is waiting for the final access token response.
' In that case, cancel the background task started in the call to StartAuth.
If (oauth2.AuthFlowState < 3) Then
oauth2.Cancel()
Debug.WriteLine("No response from the browser!")
Exit Sub
End If
' Check the AuthFlowState to see if authorization was granted, denied, or if some error occurred
' The possible AuthFlowState values are:
' 3: Completed with Success. The OAuth2 flow has completed, the background thread exited, and the successful JSON response is available in AccessTokenResponse property.
' 4: Completed with Access Denied. The OAuth2 flow has completed, the background thread exited, and the error JSON is available in AccessTokenResponse property.
' 5: Failed Prior to Completion. The OAuth2 flow failed to complete, the background thread exited, and the error information is available in the FailureInfo property.
If (oauth2.AuthFlowState = 5) Then
Debug.WriteLine("OAuth2 failed to complete.")
Debug.WriteLine(oauth2.FailureInfo)
Exit Sub
End If
If (oauth2.AuthFlowState = 4) Then
Debug.WriteLine("OAuth2 authorization was denied.")
Debug.WriteLine(oauth2.AccessTokenResponse)
Exit Sub
End If
If (oauth2.AuthFlowState <> 3) Then
Debug.WriteLine("Unexpected AuthFlowState:" & oauth2.AuthFlowState)
Exit Sub
End If
Debug.WriteLine("OAuth2 authorization granted!")
Debug.WriteLine("Access Token = " & oauth2.AccessToken)
' Get the full JSON response:
Dim json As New Chilkat.JsonObject
json.Load(oauth2.AccessTokenResponse)
json.EmitCompact = False
' The JSON response looks like this:
' {
' "token_type": "Bearer",
' "scope": "User.Read Mail.ReadWrite Mail.Send",
' "expires_in": 3600,
' "ext_expires_in": 0,
' "access_token": "EwBAA8l6B...",
' "refresh_token": "MCRMdbe...",
' "id_token": "eyJ0eXA..."
' }
' If an "expires_on" member does not exist, then add the JSON member by
' getting the current system date/time and adding the "expires_in" seconds.
' This way we'll know when the token expires.
If (json.HasMember("expires_on") <> True) Then
Dim dtExpire As New Chilkat.CkDateTime
dtExpire.SetFromCurrentSystemTime()
dtExpire.AddSeconds(json.IntOf("expires_in"))
json.AppendString("expires_on",dtExpire.GetAsUnixTimeStr(False))
End If
Debug.WriteLine(json.Emit())
' Save the JSON to a file for future requests.
Dim fac As New Chilkat.FileAccess
fac.WriteEntireTextFile("qa_data/tokens/microsoftGraph.json",json.Emit(),"utf-8",False)
Another reference for handle the logic by our self but not 3rd lib.
Accessing imgUr thru OAuth (uploading to user account)
Related
I would like to create a PostRequest in my Business Central Extension that authenticates me in my web service and returns me a token. I send my username and password in the body of the request to my web service and I also receive the token in JSON format in the body.I want to create the post request using HttpClient.
I use the following link as a template: https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/methods-auto/httpcontent/httpcontent-data-type
procedure sendPostRequest(uri: Text)
var
client: HttpClient;
content: HttpContent;
contentHeaders: HttpHeaders;
response: HttpResponseMessage;
request: HttpRequestMessage;
begin
content.GetHeaders(contentHeaders);
contentHeaders.Clear();
contentHeaders.Add('Content-Type', 'application/json');
request.Content:= content;
request.SetRequestUri(uri);
request.Method := 'POST';
end;
procedure SetURLsToDefault(var MessagingServiceSetup: Record "Messaging Service Setup WMR")
begin
MessagingServiceSetup."Service URL" := '202.212.127:8800';
end;
And I have a couple of questions:
1) the basic url is 202.212.127:8800 for my API gateway. To be able to authenticate myself I have to access 202.212.127:8800/authenticate. Is there a method in which you can create urls?
2) how do I get my username and password in the content?
3) and how do I get the token and can I save it in the field?
can someone tell me how to get the PostRequest up and running?
Common method to create different URLs is like this:
Create a setup table
Create fields like "Base Url", User, Pass etc.
I propose this pattern for your code:
SendRequest(Method; Url; Body)
Begin
...
Couple of functions (Your Api and Auth):
Authenticate()
begin
Method = 'post';
Url = SetupTable."Base Url" + '/authenticate';
Body = (Use AL Json stack and incorporate your user pass)
SendRequest(Method; Url; Body);
end;
Function1()
begin
Method = 'get';
Url = SetupTable."Base Url" + '/apiPath-Function1';
Body = '';
SendRequest(Method; Url; Body);
end
Function2()
begin
Method = 'post';
Url = SetupTable."Base Url" + '/apiPath-Function2';
Body = (Use AL Json stack and incorporate your body structure);
SendRequest(Method; Url; Body);
end;
To get your user pass into the content you need to check the documentation of the Api you're trying to call. It's usually described in details, it can be a simple header for basic authentication or a complex Jwt.
For receiving a token, again you need to check your Api documentation first, but essentially after making a Rest call (like: client.Send(RequestMessage, ResponseMessage); inside your SendRequest method), you get a response back and you can use AL Json stack to carve information out.
This is a fine article on how to proceed:
https://jackmallender.com/2019/03/04/interacting-with-rest-apis-using-json-from-within-business-central-part-1-an-introduction-to-the-httpclient-data-type/
Basically a string could work as an url. Depends on what you want. It is good practice to have a setup for your web service calls, so I am with Babak. You can set up a table in which you store the links, credentials - whatsoever.
and 4) I suggest Waldos Rest App for web service calls. you can download the source here: https://github.com/waldo1001/waldo.restapp
It encapsulated the calls, has helper functions for json handling as well. Using the "REST Helper" Codeunit. You can break down your call to:
local procedure DoCallWebservice(URI: Text; User: Text; Pass: Text; var Token: Text);
var
RESTHelper: Codeunit "REST Helper WLD";
begin
RRESTHelper.Initialize('GET', URI);
RESTHelper.SetContentType('application/json');
RESTHelper.AddBody('{"user":"USERNAME","pass":"PASSWORD"}');
if RESTHelper.Send() then
Token := RESTHelper.GetResponseContentAsText();
end;
Obviously, you need to parse the response (JSONHelper) to your needs. Look at the code of the codeunit, it's more or less self explanatory.
My goal is to download a file from Google Drive using the Drive API v3 and VB.NET.
I set up my credentials in the Google console: Under "OAuth 2.0 client IDs" I put "http://localhost" in "Authorized redirect URIs" and in "Authorized JavaScript origins" and have my client secret file. I am on ASP.NET 4.0 using NuGet package Google.Apis.Drive.v3.
The error happens on line "credential = GoogleWebAuthorizationBroker.AuthorizeAsync". It pops up a new Chrome tab and says:
That’s an error.
Error: redirect_uri_mismatch
The redirect URI in the request, http://localhost:9895/authorize/, does not match the ones authorized for the OAuth client. To update the authorized redirect URIs, visit: https://console.developers.google.com/apis/credentials/oauthclient/[MyClientID]?project=[MyProjectNumber]
However each time I get a different port number.
Public Function AuthenticateOauth() As DriveService
' Request authentication from a user using oAuth2
Dim clientSecretJson = "C:\WebApps\PeruvianGuineaPig\App_Data\client_secret.json"
Dim applicationName = "DriveApi"
Try
' Permissions
Dim scopes As String() = New String() {DriveService.Scope.DriveReadonly}
Dim credential As UserCredential
Using stream As New FileStream(clientSecretJson, FileMode.Open, FileAccess.Read)
Dim credPath As String
credPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal)
credPath = Path.Combine(credPath, ".credentials/", System.Reflection.Assembly.GetExecutingAssembly.GetName.Name)
' Requesting Authentication
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.Load(stream).Secrets,
scopes,
"user",
CancellationToken.None,
New FileDataStore(credPath, True)).Result
End Using
' Create Drive API service
Dim Service = New DriveService(New BaseClientService.Initializer() With
{.HttpClientInitializer = credential, .ApplicationName = applicationName})
Return Service
Catch ex As Exception
Return Nothing
End Try
End Function
I didn't realize I needed to open the project as a Web Site (and pick it from my list of Local IIS Sites) and not simply open the Project/Solution file. It now uses the port number I gave it in IIS when I'm debugging.
I have another issue now, but that's for another question...
Getting Access token from OAuth and passing it in the header for each of the request.
I have api at: https://login.microsoftonline.com/48b0431c-82f6-4ad2-a023-ac96dbf5614e/oauth2/token
And in Postman can see the access token getting returned.
I am trying to call this APIM api in my existing 4.5 .NET application. And wrote the following:
Dim client = InitializeTokenClient()
Using content As New StringContent(String.Empty)
Dim response = client.PostAsync($"48b0431c-82f6-4ad2-a023-ac96dbf5614e/oauth2/token", content).Result
If (response.StatusCode = HttpStatusCode.OK) Then
response.
End If
Return response
End Using
I am not sure how to access the access_token from the response object and also how would I be passing the access token to make subsequent APIM api calls?
You can take the access_token from the response by deserializing the response:
Add a reference to System.Web.Extensions and add Imports System.Web.Script.Serialization to the top of your file. Then the following code will get the access_token:
Dim j As Object = New JavaScriptSerializer().Deserialize(Of Object)(response)
Dim accessToken = j("access_token")
You can then use the token when calling the API by adding an Authorization header with the value Bearer <token>.
I set up SendGrid account and got key and pw.
My VisualBasic 2015 console app runs DeliverAsync without error, but email doesn't get to Internet receipients (my Hotmail account).
Also, the task.wait() throws exception "Bad username / password", which is posted at the end
Wireshark on Azure shows no SMTP, but I don't know if SendGrid uses SMTP.
Here is the app:
' Create the email object first, then add the properties.
Dim myMessage As SendGridMessage
myMessage = New SendGridMessage()
' Add the message properties.
myMessage.From = New MailAddress("<my email addr>")
' Add multiple addresses to the To field.
myMessage.AddTo("<destination email addr 1>")
myMessage.AddTo("<destination email addr 2>")
myMessage.AddTo("<destination email addr 3>")
myMessage.Subject = "Testing the SendGrid Library 2"
'Add the HTML and Text bodies
myMessage.Html = "<p>Hello World!</p>"
myMessage.Text = "Hello World plain text!"
Dim credentials As NetworkCredential
credentials = New NetworkCredential("apikey", "<my api pw>")
transportWeb = New Web(credentials)
Dim task = transportWeb.DeliverAsync(myMessage)
Try
task.wait()
Catch ex As AggregateException
Stop '<<<<<<<<< I GET: "Bad username / password"
Catch
End Try
EXCEPTION DETAILS:
"Bad username / password"
DeliverAsync returns a Task, so you need to await the task.
Await transportWeb.DeliverAsync(myMessage)
Of course, to use the await keyword your method needs to be marked as async. If you don't want to do that, then you can manually wait on the task.
Dim task = transportWeb.DeliverAsync(myMessage)
task.Wait()
You should familiarize yourself with the Task-based Asynchronous Pattern (TAP). Often when a function name ends in -Async then it uses TAP.
I got it working by creating new VB web app instead of win app.
VB > create new proj > web app > MVC and then props > references > NU.. Mgr > search SendGrid > Install, and that's it.
I am trying to use Net.WebRequest and Net.WebResponse to connect to a site and get the expiration date for an SSL certificate. I look through all the properties of both the request and response and can't find this information. Can anyone help?
' Initialize the WebRequest.
Dim myRequest As Net.WebRequest = Net.WebRequest.Create("https://testsite.com")
' Return the response.
Dim myResponse As Net.WebResponse = myRequest.GetResponse()
Is there a way to use the servicepoint of the WebRequest for this?