从 sign now REST API 获取访问令牌?
Get an access token from sign now REST APIs?
我遵循了这个程序。
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.
我知道这是因为错误的模式。您现在可以帮我从 sign 获取访问令牌吗?
提前致谢!!!
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();
据我所知,用户数据应该在有效载荷内发送,而不是在 header 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
对应请求的payloadPOST
.
要解决您的问题,您应该按如下所述更新您的代码:
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
中放入什么(即 user
和 password
的值),请参阅此文档 https://techlib.barracuda.com/CudaSign/RestEndpointsAPI#,部分 "Security and Access Control" .
有关详细信息,请参阅这两个链接:
- How to pass POST parameters to ASP.Net web request?
- How to send authentication header in ASP.Net for set of web request
希望对你有帮助,
蒂埃里
好的,这可能不是最优雅的解决方案,但我对这一切都是陌生的。另外,对于它在 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 的对象,我无法弄清楚创建用户。
至于 Encoded_Client_Credentials,那是由 CudSign 提供的。我目前正在尝试弄清楚如何 POST 一个文件给他们,但运气不佳。希望你过得比我轻松。
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% 确定。我添加了对我有用的代码以获得正确的响应,减去一些次要信息。让我知道这是否有帮助,如果没有,我很乐意帮助您获得正确的答复。
旁注:如果之前的尝试可以保存任何内容,则可能需要清除缓存。
旁注:如果您获得不受限制的访问令牌,则无需指定范围。
我遵循了这个程序。 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.
我知道这是因为错误的模式。您现在可以帮我从 sign 获取访问令牌吗?
提前致谢!!!
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();
据我所知,用户数据应该在有效载荷内发送,而不是在 header 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
对应请求的payloadPOST
.
要解决您的问题,您应该按如下所述更新您的代码:
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
中放入什么(即 user
和 password
的值),请参阅此文档 https://techlib.barracuda.com/CudaSign/RestEndpointsAPI#,部分 "Security and Access Control" .
有关详细信息,请参阅这两个链接:
- How to pass POST parameters to ASP.Net web request?
- How to send authentication header in ASP.Net for set of web request
希望对你有帮助, 蒂埃里
好的,这可能不是最优雅的解决方案,但我对这一切都是陌生的。另外,对于它在 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 的对象,我无法弄清楚创建用户。 至于 Encoded_Client_Credentials,那是由 CudSign 提供的。我目前正在尝试弄清楚如何 POST 一个文件给他们,但运气不佳。希望你过得比我轻松。
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% 确定。我添加了对我有用的代码以获得正确的响应,减去一些次要信息。让我知道这是否有帮助,如果没有,我很乐意帮助您获得正确的答复。
旁注:如果之前的尝试可以保存任何内容,则可能需要清除缓存。
旁注:如果您获得不受限制的访问令牌,则无需指定范围。