3

我在使用 oauth 获取客户端应用程序中的 access_token 时遇到问题。返回的响应的正文为空,但在 API 中我可以看到响应不为空。

tokenresponse = { 
   "access_token":"[ACCESSTOKENVALUE]",
   "token_type":"bearer",
   "expires_in":"1200",
   "refresh_token":"[REFRESHTOKENVALUE]",
   "scope":"[SCOPEVALUE]"
} 

API 中返回令牌http://api.sample.com/OAuth/Token的方法:

public ActionResult Token()
{
     OutgoingWebResponse response =
     this.AuthorizationServer.HandleTokenRequest(this.Request);
     string tokenresponse = string.Format("Token({0})", response!=null?response.Body:"")); 
     return response.AsActionResult();
} 

请求令牌的客户端方法是:

        public string GetAuthorizationToken(string code)
        { 
            string Url = ServerPath + "OAuth/Token"; 
            string redirect_uri_encode = UrlEncode(ClientPath);
            string param =  string.Format("code={0}&client_id={1}&client_secret={2}&redirect_uri={3}&grant_type=authorization_code",code, ClientId, ClientSecret, redirect_uri_encode);

            HttpWebRequest request = HttpWebRequest.Create(Url) as HttpWebRequest;
            string result = null;
            request.Method = "POST";
            request.KeepAlive = true;
            request.ContentType = "application/x-www-form-urlencoded";
            request.Timeout = 10000;
            request.Headers.Remove(HttpRequestHeader.Cookie);
            var bs = Encoding.UTF8.GetBytes(param);
            using (Stream reqStream = request.GetRequestStream())
            {
                reqStream.Write(bs, 0, bs.Length);
            }

            using (WebResponse response = request.GetResponse())
            {  
                var sr = new StreamReader(response.GetResponseStream());
                result = sr.ReadToEnd();
                sr.Close();
            } 
            if (!string.IsNullOrEmpty(result))
            {
                TokenData tokendata = JsonConvert.DeserializeObject<TokenData>(result);
                return UpdateAuthorizotionFromToken(tokendata);
            } 
            return null;
        } 

结果变量为空。如果您有任何想法可能导致这种情况,请告诉我。最初我认为是因为 cookie,所以我试图从请求中删除它们。提前致谢。

4

1 回答 1

0

亲爱的,只需使用以下代码创建 webclient,您将在 tokeninfo 中获得 json 信息。我使用它并且它的工作非常完美。

        WebClient client = new WebClient();
        string postData = "client_id=" + ""
                        + "&client_secret=" + ""
                        + "&grant_type=password&username=" + "" //your username
                        + "&password=" + "";//your password :)
        string soundCloudTokenRes = "https://api.soundcloud.com/oauth2/token";
        string tokenInfo = client.UploadString(soundCloudTokenRes, postData);

然后,您可以使用仅包含来自 tokeninfo 的标记的子字符串。在声音云上上传曲目。

私人无效TestSoundCloudupload(){

        System.Net.ServicePointManager.Expect100Continue = false;
        var request = WebRequest.Create("https://api.soundcloud.com/tracks") as HttpWebRequest;
        //some default headers
        request.Accept = "*/*";
        request.Headers.Add("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.3");
        request.Headers.Add("Accept-Encoding", "gzip,deflate,sdch");
        request.Headers.Add("Accept-Language", "en-US,en;q=0.8,ru;q=0.6");

        //file array
        var files = new UploadFile[] { new UploadFile(Server.MapPath("Downloads//0.mp3"), "track[asset_data]", "application/octet-stream") };
        //other form data
        var form = new NameValueCollection();
        form.Add("track[title]", "Some title");
        form.Add("track[sharing]", "public");
        form.Add("oauth_token", "");
        form.Add("format", "json");

        form.Add("Filename", "0.mp3");
        form.Add("Upload", "Submit Query");
        try
        {
            using (var response = HttpUploadHelper.Upload(request, files, form))
            {
                using (var reader = new StreamReader(response.GetResponseStream()))
                {
                    Response.Write(reader.ReadToEnd());
                }
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
    }
于 2013-01-17T10:56:11.857 回答