0

我正在使用 Unity 开发桌面应用程序,我需要检查用户是否可以登录 Outlook 以访问我的应用程序。

因此,我正在遵循 Microsoft 文档中的此过程,并试图弄清楚如何打开 Outlook API 登录页面并将授权代码发送到我的应用程序,然后获取访问令牌以最终获取有关用户的信息。我尝试了几种方法,但没有成功。

首先,我尝试通过导入一些 .dll 来获得一种 AuthenticationContext ,例如我们可以在一般的 C# .NET 应用程序中使用,但似乎这个解决方案只是一个梦想。

然后,我尝试了我发现的这个函数,但问题是 Outlook API 和所有 Microsoft Authentication API(我猜)一样,需要一个 redirect_uri 来发送代码和访问令牌。如果我能得到这个授权的代码,我想我可以使用我找到的功能来做我需要的事情。我尝试将一个带有 PHP 脚本的网站设置为 redirect_link 以获取代码,同时发出请求以获取它。但是,即使我尝试使用 UnityWebRequest 通过此脚本来获取它,它也无法正常工作,而且这确实是一种丑陋的方法^^'

有没有办法打开 Outlook“授权”页面并等待响应?就像我可以将 redirect_uri 作为我的桌面应用程序一样?

我需要你的帮助!还有比我更好的方法吗?

谢谢 !

更新:这是我的 C# 代码

private IEnumarator ConnectOutlook()
{
    string client_ID = "client-id";
    string redirect_uri = "link of my PHP script";
    string response_type = "code";
    string url = "https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=" + client_ID + "&redirect_uri=" + redirect_uri + "&response_type=" + response_type + "&scope=openid";
    Application.OpenURL(url);
    string resultCode = null;
    StartCoroutine(GetAuthorization((code) => resultCode = code));
    yield return new WaitUntil(() => resultCode != null);
StartCoroutine(GetAccessToken(resultCode));
    }

private static IEnumerator GetAuthorization(Action<string> result)
{
    Dictionary<string, string> content = new Dictionary<string, string>();

    content.Add("action", "GetCode");
    UnityWebRequest www = UnityWebRequest.Post("link of my PHP Script", content);
    //Send request
    yield return www.Send();

    yield return new WaitUntil(() => www.downloadHandler.text != "");
    if (!www.isNetworkError)
    {
        string resultContent = www.downloadHandler.text;
        Debug.Log(resultContent);
         result(resultContent);
    }
    else
    {
        //Return null
        result("");
    }
}

private static IEnumerator GetAccessToken(string code)
    {

        Dictionary<string, string> content = new Dictionary<string, string>();
        //Fill key and value

        content.Add("client_id", "client-id");
        content.Add("client_secret", "client_secret");
        content.Add("code", code);
        content.Add("redirect_uri", "link of my PHP Script");
        content.Add("grant_type", "authorization_code");

        UnityWebRequest www = UnityWebRequest.Post("https://login.microsoftonline.com/common/oauth2/v2.0/token", content);
        //Send request
        yield return www.Send();

        if (!www.isNetworkError)
        {
            string resultContent = www.downloadHandler.text;
            TokenClassName json = JsonUtility.FromJson<TokenClassName>(resultContent);

            //Return result
            Debug.Log(json.access_token);
        }
        else
        {
            //Return null
            result("");
        }
    }

public class TokenClassName
{
    public string access_token;
}

这是我的 PHP 脚本(请不要忘记我试图不使用这个脚本):

<?php
    $code = "";
  // Here I get the code when Outlook API is redirecting after log in
  if (isset($_GET['code'])) {
    //Assign the code 
        $code = $_GET['code'];
  }
  elseif (isset($_GET['error'])) {
    exit('ERROR: '.$_GET['error'].' - '.$_GET['error_description']);
  }

  //Here I try to return the assigned code
  if ($_POST["action"] == "GetCode"){
    echo $_GET['code'];
  } 
?>

我真的在寻找一种方法来禁用重定向到我的脚本。最好的方法是使用“等待”来响应 .NET UWP 中的 AuthorizationContext 以及此处解释的方式

4

1 回答 1

0

您需要使用环回重定向 URL。例如http://localhost:7000/。只需在 Unity 中运行一个监听服务器,它将监听任何对http://localhost:7000/ URL 的调用。喜欢

TcpListener server = new TcpListener ("http://localhost", 7000); 

您还可以使用一些随机会话参数来确保从刚刚启动的 propper 会话中调用http://localhost:7000 。

使用示例检查此 git-hub 存储库: https ://github.com/zizul/OAuth2.0-Unity/blob/master/Assets/OAuthClient.cs

我没有测试代码。我会在检查后更新这篇文章。但它看起来应该工作。

于 2020-04-30T16:22:27.480 回答