locked
Need help in PayPal Integration. "The remote server returned an error: (401) Unauthorized." Not calling libe API. RRS feed

  • Question

  • User726159118 posted

    Hello All,

    I have implemented the  Sandbox PayPal API in my project. I am able to complete all the steps of transaction in my application.
    Now I am using LIVE account client ID and Secret key in project. but I am not able to generate token using new clientid and secreat key.

    below is my key and value in Appsetting section of web.config file.

      <add key="clientId" value="MyClientID"/>
        <add key="clientSecret" value="MyclientSecret"/>

    Then Below is my code of Paypal class.

     public static class PaypalConfiguration
        {
            //Variables for storing the clientID and clientSecret key
            public readonly static string ClientId;
            public readonly static string ClientSecret;
    
            //Constructor
            static PaypalConfiguration()
            {
                var config = GetConfig();
                ClientId = Convert.ToString(GetWebConfigVar("clientId"));  //PaypalConfiguration["clientId"];
                ClientSecret = Convert.ToString(GetWebConfigVar("clientSecret"));// config["clientSecret"];
            }
    
            // getting properties from the web.config
            public static Dictionary<string, string> GetConfig()
            {
                return PayPal.Api.ConfigManager.Instance.GetProperties();
            }
    
            private static string GetAccessToken()
            {
                // getting accesstocken from paypal                
                //    string accessToken = new OAuthTokenCredential
                //(ClientId, ClientSecret, GetConfig()).GetAccessToken();
    
    
                string accessToken = GetPayPalAccessToken(ClientId, ClientSecret);
    
                return accessToken;
            }
    
    
    
            private static string GetPayPalAccessToken(string _ClientID, string _Secret)
            {
                string PaypalClientID = _ClientID;
                string PaypalSecret = _Secret;
    
                string AccessToken = "";
    
                try
                {
                    OAuthTokenCredential tokenCredential = new OAuthTokenCredential(PaypalClientID, PaypalSecret);
                    AccessToken = tokenCredential.GetAccessToken();
                }
                catch (PayPal.IdentityException ex)
                {
                    // ex.Details provides quick access to the API error information.
                }
                catch (PayPal.HttpException ex)
                {
                    // Some other error occurred when attempting to send the request to PayPal.
                    // ex.Response contains the full response from the API which should highlight the error encountered.
                }
                catch (PayPal.PayPalException ex)
                {
                    // A general exception was thrown from the SDK.
                }
                catch (System.Exception ex)
                {
                    // Some other general exception occurred.
                }
    
                return AccessToken;
            }
    
            public static APIContext GetAPIContext()
            {
                // return apicontext object by invoking it with the accesstoken
                APIContext apiContext = new APIContext(GetAccessToken());
                apiContext.Config = GetConfig();
                return apiContext;
            }
    
            public static string GetWebConfigVar(string strVarName)
            {
                string strRet = "";
    
                if (!string.IsNullOrEmpty(strVarName))
                {
                    try
                    {
                        strRet = ConfigurationManager.AppSettings[strVarName];
                    }
                    catch (Exception objEx)
                    {
                        //ErrorHandler.HandleExceptionNoRedirect(objEx);
                        //SystemMessaging.WriteToLog("Error retrieveing ConfigurationSettings.AppSettings value from web.config" + objEx.Message, "");
                    }
                }
                return strRet;
            }
    
    
        }

    Now I am getting following error. at  GetPayPalAccessToken function while generating token.

    "The remote server returned an error: (401) Unauthorized." While I checked in inner exception  i found that respones : "{"error":"invalid_client","error_description":"Client Authentication failed"}"   It sending request to "https://api.sandbox.paypal.com/v1/oauth2/token" still after updating the client id. Also I tried with line
    <add key="mode" value="live"/> in web config file. but still no luck.

    Please help me to resolved the issue related to Paypal access token.

    Thanks In Advance.
    Omkar.

    Monday, December 10, 2018 9:54 AM

Answers

  • User726159118 posted

    Hello All,

    MY issue got resolved by Doing the Following changes in <g class="gr_ gr_6 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins replaceWithoutSep" id="6" data-gr-id="6">Web</g> config file. 

    <configSections>
    <!--IMPORTANT: Make sure you add the configSections node to your config file so that the rest of the config file is picked by the application-->
    <section name="paypal" type="PayPal.SDKConfigHandler, PayPal" />
    
    </configSections>
    
    
    <paypal>
    <settings>
    <!-- Replace the mode to `security-test-sandbox` to test if your server supports TLSv1.2. For more information follow README instructions.-->
    <add name="mode" value="live"/>
    <add name="connectionTimeout" value="360000"/>
    <add name="requestRetries" value="1"/>
    <add name="clientId" value="MyClientID"/>
    <add name="clientSecret" value="MyclientSecret"/>
    </settings>
    </paypal>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 10, 2018 10:39 AM

All replies

  • User726159118 posted

    Hello All,

    MY issue got resolved by Doing the Following changes in <g class="gr_ gr_6 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins replaceWithoutSep" id="6" data-gr-id="6">Web</g> config file. 

    <configSections>
    <!--IMPORTANT: Make sure you add the configSections node to your config file so that the rest of the config file is picked by the application-->
    <section name="paypal" type="PayPal.SDKConfigHandler, PayPal" />
    
    </configSections>
    
    
    <paypal>
    <settings>
    <!-- Replace the mode to `security-test-sandbox` to test if your server supports TLSv1.2. For more information follow README instructions.-->
    <add name="mode" value="live"/>
    <add name="connectionTimeout" value="360000"/>
    <add name="requestRetries" value="1"/>
    <add name="clientId" value="MyClientID"/>
    <add name="clientSecret" value="MyclientSecret"/>
    </settings>
    </paypal>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 10, 2018 10:39 AM
  • User1520731567 posted

    Hi Omkar Mhaiskar,

    I am glad that you have solved your problem,and post your solution.

    I suggest you could mark yourself so that more people can see your answer to get help.

    Best Regards.

    Yuki Tao

    Tuesday, December 11, 2018 2:40 AM