這不是一個問題,而是一個答案。我想我會想與你分享這件事,因爲我有點困惑,發現在ebay OAuth 2.0和c#web應用程序結合中發揮得如此之大。如何從eBay通過oauth 2.0獲取訪問令牌c#
我試着開始使用RESTsharp庫,但卡在創建正文內容的地方。 RESTsharp更喜歡XML或JSON,易趣需要一個帶有參數的字符串。
因此,如果遇到同樣的問題,爲了給您一點幫助,我決定發佈我的解決方案(不使用RESTsharp)。
public class HomeController : Controller {
string clientId = "YOUR_CLIENT_ID";
string clientSecret = "YOUR_CLIENT_SECRET";
string ruName = "YOUR_RU_NAME";
//將請求重定向獲得令牌
public ActionResult Index() {
var authorizationUrl =
"https://signin.sandbox.ebay.de/authorize?" +
"client_id=" + clientId + "&" +
"redirect_uri=" + ruName + "&" +
"response_type=code";
Response.Redirect(authorizationUrl);
return View();
}
請求//我用測試的方法在控制測試的結果,在這裏使用您的apropriate方法
public ActionResult Test(string code)
{
ViewBag.Code = code;
// Base 64 encode client Id and client secret
var clientString = clientId + ":" + clientSecret;
byte[] clientEncode = Encoding.UTF8.GetBytes(clientString);
var credentials = "Basic " + System.Convert.ToBase64String(clientEncode);
HttpWebRequest request = WebRequest.Create("https://api.sandbox.ebay.com/identity/v1/oauth2/token")
as HttpWebRequest;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.Headers.Add(HttpRequestHeader.Authorization, credentials);
var codeEncoded = HttpUtility.UrlEncode(code);
var body = "grant_type=authorization_code&code=" + codeEncoded + "&redirect_uri=" + ruName;
// Encode the parameters as form data
byte[] formData = UTF8Encoding.UTF8.GetBytes(body);
request.ContentLength = formData.Length;
// Send the request
using (Stream post = request.GetRequestStream())
{
post.Write(formData, 0, formData.Length);
}
// Pick up the response
string result = null;
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
StreamReader reader = new StreamReader(response.GetResponseStream());
result = reader.ReadToEnd();
}
ViewBag.Response = result;
return View();
}
如果輸出ViewBag.Response,您將看到授權碼。玩的開心。
將您的邏輯移出控制器和模型。然後他們可以在庫中使用,而這些庫又可以在其他項目中使用,無論是控制檯,Web還是wpf/winforms。 – user3791372
也是新的文檔:http://developer.ebay.com/devzone/rest/sell/content/selling-ig/dev-app.html,它給出了一個更清晰,簡明的如何得到鑰匙和更多的全面解釋細目。 – user3791372
在http://developer.ebay.com/devzone/rest/ebay-rest/content/oauth-quick-ref-user-tokens.html是關於它的一些材料。我沒有在你的「ActionResult Index()」範圍中找到。沒有它,你獲得了corect令牌嗎? –