Paypal c# REST API asks for an undocumented configuration section

早过忘川 提交于 2019-12-04 11:33:30

I was running into this same error. I tried Skliwz's solution but it did not work for me.

Instead I was able to get a result by passing a dictionary object with the call.

Dictionary<string, string> payPalConfig = new Dictionary<string, string>();
        payPalConfig.Add("mode", "sandbox");
OAuthTokenCredential tokenCredential = new AuthTokenCredential("myCliedId", "myClientSecret", payPalConfig);
string accessToken = tokenCredential.GetAccessToken();

Still working on get my Log In to work...

I've worked this out with the support of a PayPal dev. One needs to add:

<configSections>
  <section name="paypal" type="PayPal.Manager.SDKConfigHandler, PayPalCoreSDK"/>
</configSections>
<paypal>
  <accounts>
    <account apiUsername="xxx"
             apiPassword="yyy"
             applicationId="APP-80W284485P519543T"
             apiSignature="zzz"
             />
  </accounts>
  <settings>
    <add name="mode" value="sandbox"/>
  </settings>
</paypal>

where xxx, yyy, zzz you are values that you get from the "Account details" of your main sandbox test account.

If you're using PayPal .Net SDK (mine is version 1.3.0) you just need the following:

<configSections>
    <section name="paypal" type="PayPal.SDKConfigHandler, PayPal" />
</configSections>
<paypal>
    <settings>
       <add name="mode" value="sandbox" />
    </settings>
</paypal>

If you're like me, and don't want to store the client information in the *.config file (web.config, app.config) I've found you can specify it in a Dictionary which you have to pass-in to OAuthTokenCredential AND assigned to the APIContext.Config (key to working):

var clientId = "___REPLACE_WITH_CLIENTID___";
var clientSecret = "___REPLACE_WITH_CLIENTSECRET___";            
var sdkConfig = new Dictionary<string, string> {
   { "mode", "sandbox" },
   { "clientId", clientId },
   { "clientSecret", clientSecret }
};
var accessToken = new OAuthTokenCredential(clientId, clientSecret, sdkConfig).GetAccessToken();
var apiContext = new APIContext(accessToken);
apiContext.Config = sdkConfig;

Seems a bit redundant to have to pass it into OAuthTokenCredential and set it to apiContext.Config, but that's what works for me.

Just for future reference, the available config settings for the PayPal .NET SDK are now provided on the SDK's GitHub wiki. This includes information on what all the supported PayPal config settings are and their default values.

The wiki also includes information on how to (optionally) setup log4net in the config if you'd like to enable logging with your application.

If any information is missing or needs clarification, or if you'd like to request support for more config settings, please don't hesitate to let me know here or on GitHub.

var config = ConfigManager.Instance.GetProperties();

        // Use OAuthTokenCredential to request an access token from PayPal
        var accessToken = new OAuthTokenCredential(config).GetAccessToken();

Web config:

    <configuration>
  <configSections>
    <section name="paypal" type="PayPal.SDKConfigHandler, PayPal" />
  </configSections>

  <!-- PayPal SDK settings -->
  <paypal>
    <settings>
      <add name="mode" value="sandbox"/>
      <add name="clientId" value="_client_Id_"/>
      <add name="clientSecret" value="_client_secret_"/>
    </settings>
  </paypal>
</configuration>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!