如何在C#中发布来自Filemaker API的授权令牌请求

时间:2019-06-26 11:31:16

标签: c# http post authorization

我遇到了对FileMaker API的POST请求,以获取授权令牌,但会引发网络异常。我该如何解决?

我已经在Java中尝试过。现在我正在c#.NET Framework 4.6.1中进行尝试

private static void GetToken(string username, string password)
{
    try
    {
        var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://exchange.furniflair.com/fmi/data/v1/databases/FurniflairDB/sessions");
        httpWebRequest.ContentType = "application/json";
        httpWebRequest.Method = "POST";
        var bytes = Encoding.UTF8.GetBytes("dataapi:Data4me");
        string temp =  Convert.ToBase64String(bytes);
        httpWebRequest.Headers.Add("Authorization", "Basic ZGF0YWFwaTpEYXRhNG1l");
        string token;
        using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
        {
             streamWriter.Flush();
        }
        var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            token = streamReader.ReadToEnd();
        }
    }
    catch (WebException ex){}
}

我希望输出是作为响应的授权令牌,但是我只收到像httpWebException这样的异常。

1 个答案:

答案 0 :(得分:0)

如果可以帮助您正确设置标头,则下面是使用PHP的示例。

$Post_Json = '{}';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$URL);
curl_setopt($ch, CURLOPT_PORT,443);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout after 30 seconds
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $Post_Json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result=curl_exec ($ch);

这来自:https://schwarzsoftware.com.au/blogentry.php?id=19 还要确保在FileMaker Admin服务器控制台中打开了Data API,并为您尝试访问的FileMaker文件启用了数据API,并确保在FileMaker文件中为该用户启用了特权。