Debug my Remote Event Receiver using Ngrok will return this error "The remote server returned an error: (401) Unauthorized. - {"error":"invalid_client","error_description":"AADSTS70002:"

john john 926 Reputation points
2021-04-13T17:51:00.387+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 and the remote event receiver got fired i will get this error inside the Trace.TraceInformation("GetAppOnlyContext failed. {0}", ex.Message);:-

The remote server returned an error: (401) Unauthorized. - {"error":"invalid_client","error_description":"AADSTS70002: Client application name '**b@' is not valid or the credentials used to authenticate the client could not be understood by the server.\r\nTrace ID: dadc7c0c-5d34-4b2f-9808-17308a6b5f00\r\nCorrelation ID: 25fcaf06-5418-41dd-bf78-b6e289af8ea4\r\nTimestamp: 2021-04-13 17:42:39Z","error_codes":[70002],"timestamp":"2021-04-13 17:42:39Z","trace_id":"dadc7c0c-5d34-4b2f-9808-17308a6b5f00","correlation_id":"25fcaf06-5418-41dd-bf78-b6e289af8ea4","error_uri":"https://accounts.accesscontrol.windows.net/error?code=70002"}  

so can anyone advice on this ? why i am getting Client application name '****@' is not valid 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>  

Also inside the GetRealmFromTargetUrl method will get this error that the connection was closed by target machine:-

87481-fetrealmfromtargeturl.png

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,542 questions
0 comments No comments
{count} votes