question

johnjohn-0472 avatar image
0 Votes"
johnjohn-0472 asked Jerryzy commented

Unable to debug my Remote Event Reciever locally using Ngrok

Last month the below steps were working well for me to debug and test a remote event receiver locally:-


1) Open Ngrok.exe >> run the following command inside ngrok:-

 ngrok authtoken 3***e
 ngrok http --host-header=rewrite  57269

2) register a new app:- @ https://.sharepoint.com/sites//_layouts/15/AppRegNew.aspx >> enter the above Ngrok.exe urls inside the App Redirect URL & App Domain

3) Inside the "_layouts/15/appinv.aspx" >> i search for the above app using client ID >> and enter the following:-

 <AppPermissionRequests AllowAppOnlyPolicy="true">
   <AppPermissionRequest Scope="http://sharepoint/content/tenant" Right="FullControl" />
 </AppPermissionRequests>


5) update the service 's web config with the above client id and client secret

6) register the new remove event receiver as follow:-

 Add-PnPEventReceiver -List "Order Management" -Name "TasksRER" -Url http://cc6e945e82f6.ngrok.io/service1.svc -EventReceiverType ItemUpdated -Synchronization Asynchronous


but today when i tried the above steps it failed >> where inside my event receiver when i tried to get the context >> i will get that the Context is null:-

  public void ProcessOneWayEvent(SPRemoteEventProperties properties)
         {
             var prop = properties;
             var listItemID = properties.ItemEventProperties.ListItemId;
             var listTitle = properties.ItemEventProperties.ListTitle;
             using (ClientContext context = Helpers.GetAppOnlyContext(properties.ItemEventProperties.WebUrl))
             {
                 context.Load(context.Web);
                 context.ExecuteQuery();


Here is a screen shot from Visual Studio with the error i am getting when trying to get the context:-

85719-exce.png


Any advice if anything has been changed which is preventing me from running the above steps? which were working well last month?
Thanks

here is the code for the GetAppOnlyContext

     public class Helpers
         {
             public static ClientContext GetAppOnlyContext(string siteUrl)
             {
                 try
                 {
                     Uri siteUri = new Uri(siteUrl);
                     string realm = TokenHelper.GetRealmFromTargetUrl(siteUri);
                     string accessToken = TokenHelper.GetAppOnlyAccessToken(TokenHelper.SharePointPrincipal, siteUri.Authority, realm).AccessToken;
        
                     return TokenHelper.GetClientContextWithAccessToken(siteUri.ToString(), accessToken);
                 }
        
                 catch (Exception ex)
                 {
                     Trace.TraceInformation("GetAppOnlyContext failed. {0}", ex.Message);
                 }
                 return null;
             }
        
             public static ClientContext GetAuthenticatedContext(string siteUrl)
             {
                 string userName = WebConfigurationManager.AppSettings.Get("AuthenticatedUserName");
                 string password = WebConfigurationManager.AppSettings.Get("AuthenticatedUserPassword");
                 return GetAuthenticatedContext(siteUrl, userName, password);
             }
        
             public static ClientContext GetAuthenticatedContext(string siteUrl, string userName, SecureString password)
             {
                 ClientContext ctx = new ClientContext(siteUrl);
                 ctx.Credentials = new SharePointOnlineCredentials(userName, password);
                 return ctx;
             }
        
             public static ClientContext GetAuthenticatedContext(string siteUrl, string userName, string password)
             {
                 SecureString securePassword = GetPassword(password);
                 return GetAuthenticatedContext(siteUrl, userName, securePassword);
             }
        
             private static SecureString GetPassword(string passwd)
             {
                 var secure = new SecureString();
                 foreach (char c in passwd)
                 {
                     secure.AppendChar(c);
                 }
                 return secure;
             }
        
             public static string EmptyIfNull(object obj)
             {
                 return obj == null ? "" : obj.ToString();
             }
         }
     }  


office-sharepoint-online
exce.png (29.3 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Jerryzy avatar image
0 Votes"
Jerryzy answered Jerryzy commented

What's the details of Helpers.GetAppOnlyContext() ? Seems it's a custom method and a custom Helpers class.

· 11
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@Jerryzy-MSFT it is the same as the one found in regular remote event receiver,, i editted my question with the full code of this method.


0 Votes 0 ·

The ClientContext is got with AccessToken, can you check if the access token is valid ? Add a breakpoint in to do a check:

string accessToken = TokenHelper.GetAppOnlyAccessToken(TokenHelper.SharePointPrincipal, siteUri.Authority, realm).AccessToken;

0 Votes 0 ·

@Jerryzy-MSFT seem this statement string accessToken = TokenHelper.GetAppOnlyAccessToken(TokenHelper.SharePointPrincipal, siteUri.Authority, realm).AccessToken; will raise this error token request failed:-


86307-error.png


0 Votes 0 ·
error.png (39.5 KiB)

Did you grant the permission via the Admin Center Url below, as it needs the Tenant Full Control Permission:

https://TenantName-admin.sharepoint.com/_layouts/15/appinv.aspx

86708-sharepointapponly2.png


Reference:

Granting access using SharePoint App-Only


0 Votes 0 ·

@Jerryzy-MSFT yes i already did that,, the error i am getting is not related to permission,, but rather is related that the context is null...

0 Votes 0 ·

I see the TokenHelper.GetAppOnlyAccessToken is returning 401, so I confirm with the App Permission, as the ClientContext is got based on the access token, if the permission not set correctly, then also will return the 401 status.

Now can you create a new App for a try ? Basically, make sure the access token is valid otherwise ClientContext will be null.

0 Votes 0 ·

@Jerryzy-MSFT i created a new APP and tried on another site, still the same issue... as i mentioned the above steps use to work well for me last month,, so could the reason that something has changed on sharepoint online,, which is preventing remote debugging? also if i deploy the same code to an actual app and i add the app to the app catalog the RER will work fine... so i think the problem is with the integration between ngrok and SharePoint online

0 Votes 0 ·

ngrok is the third party debug tool, so Microsoft have no documentation specify the changes with integration between ngrok and SharePoint Online recently.

0 Votes 0 ·

@Jerryzy-MSFT I use Ngrok as i were advised from other questions to use it to be able to debug the remote event receivers.. not sure how i can debug this RER then?

0 Votes 0 ·

@Jerryzy-MSFT any further advice on this?
Thanks

0 Votes 0 ·
Show more comments