2016-10-05 89 views
0

我嘗試通過Xamarin Forms使用PayPal RESTful API,並在嘗試獲取OAuth令牌時遇到麻煩。這是我正在運行的代碼,並且在Android上發生錯誤。我在這裏更改了身份驗證標頭,以便我的客戶端ID和密碼不可公開提供。這也是我基於這個HTTP請求的curl命令:https://developer.paypal.com/docs/integration/direct/make-your-first-call/。謝謝。當通過Httpclient提交HTTP請求時System.TypeLoadException

var getToken = new HttpClient(new NativeMessageHandler()); 
getToken.BaseAddress = new Uri("https://api.sandbox.paypal.com/v1/oauth2/token"); 
getToken.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 
getToken.DefaultRequestHeaders.AcceptLanguage.Add(new StringWithQualityHeaderValue("en_US")); 
getToken.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("**client_id**", "**secret**"); 
getToken.DefaultRequestHeaders.TryAddWithoutValidation("content-type", "application/x-www-form-urlencoded"); 

OAuthDetails clientCredentials = new OAuthDetails("client_credentials"); 
HttpResponseMessage tokenResponse = await getToken.PostAsJsonAsync("/v1/oauth2/token", clientCredentials); 
AccessResponse accessInfo = await tokenResponse.Content.ReadAsAsync<AccessResponse>(); 

回答

0

發送與應用程序/ x-WWW的形式urlencoded的POST請求的方法是這樣的:而不是TryAddWithoutValidation的,你需要做到這一點,那麼包括本作中請求而不是客戶端證書的內容就像我以前所做的那樣

var tokenContent = new FormUrlEncodedContent(new[] 
{ 
    new KeyValuePair<string, string>("grant_type", "client_credentials" 
});