从sign now REST API获取访问令牌?

时间:2015-03-27 06:50:31

标签: c# asp.net rest httpwebrequest httpwebresponse

我遵循了这个程序。 https://techlib.barracuda.com/CudaSign/RestEndpointsAPI

这是获取访问令牌的C#代码。

        string userData = "username=email@domain.com&password=mypassword&grant_type=password";
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://signnow.mydomain.com/api/index.php/oauth2/token");
        request.Accept = "application/json";
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        request.Headers.Add("Authorization", "Basic " + userData);

        var response = request.GetResponse() as HttpWebResponse;

        if (response.StatusCode == HttpStatusCode.OK)
        {
               //JSON output.
        }

我收到以下错误:

      The remote server returned an error: (400) Bad Request.

我知道这是因为错误的模式。你能帮助我现在从登录获取访问令牌吗?

提前致谢!!!

cURL请求:

string data = "username=email@domain.com&password=mypassword&grant_type=password";

 WebRequest myReq = WebRequest.Create(myURL + "oauth2/token");
 myReq.Method = "POST";
 //myReq.ContentLength = data.Length;
 myReq.ContentType = "application/x-www-form-urlencoded";

 UTF8Encoding enc = new UTF8Encoding();

//myReq.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(enc.GetBytes(data)));
 myReq.Headers.Add("Authorization", "Basic " + data);
 WebResponse wr = myReq.GetResponse();

3 个答案:

答案 0 :(得分:1)

据我所知,用户数据应该在有效载荷内发送,而不是在标题Authorization内发送。客户端凭据(ENCODED_CLIENT_CREDENTIALS)必须与您在Barracuda上的全局帐户相关联。

我建议您使用curl测试您的请求,因为该工具的文档使用它:

curl -H 'Authorization: Basic ENCODED_CLIENT_CREDENTIALS'
   --data 'username=user@test.com&password=test&grant_type=password&scope=user%20documents%20user%2Fdocumentsv2' https://capi-eval.signnow.com/api/oauth2/token

命令参数--data对应于请求POST的有效负载。

要解决您的问题,请按照以下说明更新您的代码:

string encodedUserCredentials = 
    Convert.ToBase64String(
       System.Text.ASCIIEncoding.ASCII.GetBytes("user:password")));
string userData = "username=email@domain.com&password=mypassword&grant_type=password";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://signnow.mydomain.com/api/index.php/oauth2/token");
request.Accept = "application/json";
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.Headers.Add("Authorization", "Basic " + encodedUserCredentials);

StreamWriter requestWriter = new StreamWriter(request.GetRequestStream());
requestWriter.Write(userData);
requestWriter.Close();

var response = request.GetResponse() as HttpWebResponse;

要了解变量encodedUserCredentials中的内容(即userpassword的值),请参阅此文档https://techlib.barracuda.com/CudaSign/RestEndpointsAPI#,“安全和访问控制”部分。

有关详细信息,请参阅以下两个链接:

希望它可以帮到你, 亨利

答案 1 :(得分:0)

好的,这可能不是最优雅的解决方案,但我对这一切都不熟悉。我也为它在vb而不是C#

道歉
Public Class iqAPI
Public Shared Function postRequest(ByVal url As String, ByVal toSerialize As String, strHeader As String) As DataTable
    Dim wHeader As WebHeaderCollection = New WebHeaderCollection
    wHeader.Clear()
    wHeader.Add(strHeader)
    Dim wReq As WebRequest = WebRequest.Create(url)
    Dim postData As String = JsonConvert.SerializeObject(toSerialize)
    Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
    wReq.Headers = wHeader
    wReq.Method = "POST"
    wReq.ContentType = "application/x-www-form-urlencoded"
    wReq.ContentLength = byteArray.Length

Dim dataStream As Stream = wReq.GetRequestStream()
dataStream.Write(byteArray, 0, byteArray.Length)
dataStream.Close()

Dim wResp As WebResponse = wReq.GetResponse()

MsgBox(CType(wResp, HttpWebResponse).StatusDescription)
dataStream = wResp.GetResponseStream()
Using reader As New StreamReader(dataStream)
    Dim respFromServer As String = reader.ReadToEnd()
    Dim dtCudaClient As DataTable = JsonConvert.DeserializeObject(Of DataTable)("[" & respFromServer & "]")
    MsgBox(dtCudaClient.Rows(0).ToString)
    iqSTAMP.gvCudaClients.DataSource = dtCudaClient
    reader.Close()
    dataStream.Close()
    wResp.Close()
    Return dtCudaClient
End Using
Return Nothing
End Function

有几点要注意,我重载了这个以使用对象而不是toSerialize的字符串。在您创建用户时,您必须以json格式使用它,并且当您获得令牌时,您使用上述方法以您拥有的方式传递字符串。如果没有将序列化为json的对象,我无法找出创建用户。 至于CodedSign提供的Encoded_Client_Credentials。我目前正试图弄清楚如何将文件发布给他们而没有太多运气。希望你比我更容易。

答案 2 :(得分:0)

var client = new RestClient("https://api-eval.signnow.com/oauth2/token");
var request = new RestRequest(Method.POST);
request.AddHeader("content-type", "application/x-www-form-urlencoded");
request.AddHeader("authorization", "Basic BASE64_ENCODED_CLIENT_ID:CLIENT_SECRET");
request.AddParameter("application/x-www-form-urlencoded", "username=EMAIL&password=PASSWORD&grant_type=password", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);

看起来你正在给我错误的终点,但不是100%肯定。我添加了适合我的代码,以获得正确的响应,减去一些小的信息。如果有帮助,请告诉我,如果不是,我很乐意帮助您做出正确的回应。

附注:如果可以从之前的尝试中保存任何内容,则可能需要清除缓存。

附注:如果您获得了不受限制的访问令牌,则无需指定范围。