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

john john 946 Reputation points
2022-05-26T01:50:29.547+00:00

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>  
SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,742 questions
SharePoint Server Development
SharePoint Server Development
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Development: The process of researching, productizing, and refining new or existing technologies.
1,576 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Tong Zhang_MSFT 9,116 Reputation points
    2022-05-26T08:49:23.51+00:00

    Hi @john john ,

    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.