Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当我指向我的URL时,请求无效http://localhost:59185/api/values ... 希望取回我的访问令牌,而不是获取错误_C#_.net_Asp.net Web Api_Access Token - Fatal编程技术网

C# 当我指向我的URL时,请求无效http://localhost:59185/api/values ... 希望取回我的访问令牌,而不是获取错误

C# 当我指向我的URL时,请求无效http://localhost:59185/api/values ... 希望取回我的访问令牌,而不是获取错误,c#,.net,asp.net-web-api,access-token,C#,.net,Asp.net Web Api,Access Token,我在VisualStudio中有一个Web API项目,我正在将Web URL重定向到我的本地主机,我有我的模型类和值控制器,目前正在尝试从我的API获取响应 邮递员图像(邮递员如何获取凭证) 在运行此项目时,我在计算机上遇到一条错误消息 URL-> 错误消息: <string xmlns="http://schemas.microsoft.com/2003/10/Serialization/"> {"error":"invalid_request","error_descripti

我在VisualStudio中有一个Web API项目,我正在将Web URL重定向到我的本地主机,我有我的模型类和值控制器,目前正在尝试从我的API获取响应

邮递员图像(邮递员如何获取凭证)

在运行此项目时,我在计算机上遇到一条错误消息 URL->

错误消息:

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">
{"error":"invalid_request","error_description":"grant_type is required for issuance"}
</string>

{“错误”:“无效的请求”,“错误描述”:“颁发需要授权类型”}
它告诉我,授权类型是发行所必需的,但很明显,我已将其标识为我的Valuescontroller类

using APICredential.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Script.Serialization;

namespace APICredential.Controllers
{
    [RoutePrefix("api")]
    public class ValuesController : ApiController
    {

        [HttpGet, Route("values")]
        public async Task<string> Post()
        {
            Credentials cred = new Credentials()
            {

                username = "admin@encompass:BE11200822",
                password = "S*******",
                grant_type = "password", //Gran_type Identified here
                client_id = "gpq4sdh",
                client_secret = "secret",
                scope = "lp"
            };

            try {
                using (HttpClient client = new HttpClient())
                {
                    client.BaseAddress = new Uri("https://api.elliemae.com/oauth2/");

                    HttpRequestMessage request = new HttpRequestMessage
                    (HttpMethod.Post, "v1/token")
                    {
                        Content = new StringContent(new JavaScriptSerializer().Serialize(cred), Encoding.UTF8, "application/x-www-form-urlencoded")
                    };

                    HttpResponseMessage response = await client.SendAsync(request);

                    //for now, see what response gets you and adjust your code to return the object you need, if the api is returning a serialized json string.. then we can return a string here... like so

                    string result = await response.Content.ReadAsStringAsync();

                    return result;

                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }


        [HttpGet, Route("values/{id}")]
        public string Get(int id)
        {
            return "Nice! At least we got this one working, Fay... the Id value you entered is: " + id.ToString();
        }
使用APICredential.Models;
使用制度;
使用System.Collections.Generic;
使用System.Linq;
Net系统;
使用System.Net.Http;
使用系统文本;
使用System.Threading.Tasks;
使用System.Web.Http;
使用System.Web.Script.Serialization;
命名空间APICredential.Controllers
{
[RoutePrefix(“api”)]
公共类值控制器:ApiController
{
[HttpGet,路由(“值”)]
公共异步任务Post()
{
凭证cred=新凭证()
{
用户名=”admin@encompass:BE11200822“,
password=“S*******”,
grant_type=“password”,//此处标识的Gran_类型
客户机\u id=“gpq4sdh”,
客户_secret=“secret”,
scope=“lp”
};
试一试{
使用(HttpClient=new HttpClient())
{
client.BaseAddress=新Uri(“https://api.elliemae.com/oauth2/");
HttpRequestMessage请求=新建HttpRequestMessage
(HttpMethod.Post,“v1/token”)
{
Content=new-StringContent(new-JavaScriptSerializer().Serialize(cred),Encoding.UTF8,“application/x-www-form-urlencoded”)
};
HttpResponseMessage response=等待客户端.SendAsync(请求);
//现在,看看得到了什么样的响应,并调整代码以返回所需的对象,如果api返回的是序列化的json字符串,那么我们可以在这里返回一个字符串……如下所示
字符串结果=wait response.Content.ReadAsStringAsync();
返回结果;
}
}
捕获(例外情况除外)
{
抛出新异常(例如消息);
}
}
[HttpGet,Route(“values/{id}”)]
公共字符串Get(int-id)
{
return“很好!至少我们让它工作了,费伊……您输入的Id值是:”+Id.ToString();
}

查看您的Postman屏幕截图,您似乎将内容错误地传递给API。应该是这样的:

namespace APICredential.Controllers
{
    [RoutePrefix("api")]
    public class ValuesController : ApiController
    {

        [HttpGet, Route("values")]
        public async Task<string> Post()
        {
            using (HttpClient client = new HttpClient())
            {
                client.BaseAddress = new Uri("https://api.elliemae.com/oauth2/");



                var parameters = new Dictionary<string, string>()
                {
                    {"username", "admin@encompass:BE11200822"},
                    {"password ", "S*******"},
                    {"grant_type", "password"}, //Gran_type Identified here
                    {"client_id", "gpq4sdh"},
                    {"client_secret", "secret"},
                    {"scope", "lp"}
                };

                HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "v1/token")
                {
                    Content = new FormUrlEncodedContent(parameters)
                };

                HttpResponseMessage response = await client.SendAsync(request);

                string result = await response.Content.ReadAsStringAsync();

                return result;
            }
        }
    }
}
namespace APICredential.Controllers
{
[RoutePrefix(“api”)]
公共类值控制器:ApiController
{
[HttpGet,路由(“值”)]
公共异步任务Post()
{
使用(HttpClient=new HttpClient())
{
client.BaseAddress=新Uri(“https://api.elliemae.com/oauth2/");
var参数=新字典()
{
{“用户名”admin@encompass:BE11200822“},
{“密码”,“S*******”},
{“grant\u type”,“password”},//此处标识的Gran\u类型
{“客户机id”,“gpq4sdh”},
{“客户机密”,“机密”},
{“范围”,“lp”}
};
HttpRequestMessage请求=新的HttpRequestMessage(HttpMethod.Post,“v1/token”)
{
内容=新的FormUrlEncodedContent(参数)
};
HttpResponseMessage response=等待客户端.SendAsync(请求);
字符串结果=wait response.Content.ReadAsStringAsync();
返回结果;
}
}
}
}

查看您的Postman屏幕截图,您似乎将内容错误地传递给API。应该是这样的:

namespace APICredential.Controllers
{
    [RoutePrefix("api")]
    public class ValuesController : ApiController
    {

        [HttpGet, Route("values")]
        public async Task<string> Post()
        {
            using (HttpClient client = new HttpClient())
            {
                client.BaseAddress = new Uri("https://api.elliemae.com/oauth2/");



                var parameters = new Dictionary<string, string>()
                {
                    {"username", "admin@encompass:BE11200822"},
                    {"password ", "S*******"},
                    {"grant_type", "password"}, //Gran_type Identified here
                    {"client_id", "gpq4sdh"},
                    {"client_secret", "secret"},
                    {"scope", "lp"}
                };

                HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "v1/token")
                {
                    Content = new FormUrlEncodedContent(parameters)
                };

                HttpResponseMessage response = await client.SendAsync(request);

                string result = await response.Content.ReadAsStringAsync();

                return result;
            }
        }
    }
}
namespace APICredential.Controllers
{
[RoutePrefix(“api”)]
公共类值控制器:ApiController
{
[HttpGet,路由(“值”)]
公共异步任务Post()
{
使用(HttpClient=new HttpClient())
{
client.BaseAddress=新Uri(“https://api.elliemae.com/oauth2/");
var参数=新字典()
{
{“用户名”admin@encompass:BE11200822“},
{“密码”,“S*******”},
{“grant\u type”,“password”},//此处标识的Gran\u类型
{“客户机id”,“gpq4sdh”},
{“客户机密”,“机密”},
{“范围”,“lp”}
};
HttpRequestMessage请求=新的HttpRequestMessage(HttpMethod.Post,“v1/token”)
{
内容=新的FormUrlEncodedContent(参数)
};
HttpResponseMessage response=等待客户端.SendAsync(请求);
字符串结果=wait response.Content.ReadAsStringAsync();
返回结果;
}
}
}
}

这是文档中创建令牌的方式吗?将凭据序列化为消息正文中的JSON?您确定API不希望
grant_type
出现在标题中吗?让我拍一张邮递员的照片,向您展示邮递员拿走了什么,我将对此进行编辑您的API需要
应用程序/x-www-form-urlencoded
如果您在标题中设置了正确的内容类型,那么您将发布一个JSON字符串,因为
JavascriptSerializer
创建JSON。也许这个问题有助于正确创建您的正文