question

johnjohn-0472 avatar image
0 Votes"
johnjohn-0472 asked TongZhangMSFT-7548 commented

Debug my Remote Event Receiver using Ngrok will return this error "The remote server returned an error: (401) Unauthorized.

I have the following code inside my remote event receiver:-

  public void ProcessOneWayEvent(SPRemoteEventProperties properties)
         {
             int listItemID = properties.ItemEventProperties.ListItemId;
             var listname = properties.ItemEventProperties.ListTitle;
             string siteUrl = properties.ItemEventProperties.WebUrl;
             using (ClientContext context = Helpers.GetAppOnlyContext(siteUrl))
             {

and the Helpers.GetAppOnlyContext is :-

         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();
         }
     }

now when i add a new item >> the remote event receiver will get fired, but i will get this error :-

 The remote server returned an error: (401) Unauthorized. 

205677-image.png

so can anyone advice on this ? Although i have generated the ClientID & ClientSecret using the sharepoint app registration page @ _layouts/15/AppRegNew.aspx and i added the following permision inside the AppInv.aspx page:-

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


office-sharepoint-onlineoffice-sharepoint-server-development
image.png (35.4 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

TongZhangMSFT-7548 avatar image
0 Votes"
TongZhangMSFT-7548 answered TongZhangMSFT-7548 commented

Hi @johnjohn-0472,

Based on my research, I recommend you do a troubleshooting as suggested in the following article, and check if the error still exists.

Article:

https://www.spguides.com/the-remote-server-returned-an-error-401-unauthorized-sharepoint/

Note: Microsoft is providing this information as a convenience to you. The sites are not controlled by Microsoft. Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. Please make sure that you completely understand the risk before retrieving any suggestions from the above link.


If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.




· 1
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.

Hi @johnjohn-0472,

I am checking to see if the problem has been resolved .If you have any questions or progress, you can contact me in time.

0 Votes 0 ·