简体   繁体   English

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

[英]How to POST request for authorization token from filemaker API in c#

I am hitting a POST request to a FileMaker API to get an authorization token but its throwing web exception. 我遇到了对FileMaker API的POST请求,以获取授权令牌,但会引发网络异常。 How can I fix it? 我该如何解决?

I have tried it in java. 我已经在Java中尝试过了。 Now I am trying it in c# .NET framework 4.6.1 现在我正在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){}
}

I expect the output to be the authorization token in response, but I am getting only exceptions like httpWebException. 我希望输出是响应中的授权令牌,但是我仅收到像httpWebException这样的异常。

If it helps you set the headers correctly, here is an example that works using PHP. 如果它可以帮助您正确设置标题,那么以下是一个使用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);

This comes from: https://schwarzsoftware.com.au/blogentry.php?id=19 Also ensure that the Data API is turned on in the FileMaker Admin server console, and enabled for the FileMaker file you are trying to access, and access priviliges enabled for that user in the FileMaker file. 这来自: https : //schwarzsoftware.com.au/blogentry.php?id=19还要确保在FileMaker Admin服务器控制台中打开了Data API,并为您尝试访问的FileMaker文件启用了该API,并且访问FileMaker文件中为该用户启用的特权。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM