【问题标题】:OAuth2 : How to get authorization code from Outlook API for Authentication in Unity?OAuth2:如何从 Outlook API 获取授权代码以在 Unity 中进行身份验证?
【发布时间】:2018-02-23 22:59:39
【问题描述】:

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

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

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

然后,我尝试了这个functions,但问题是 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 以及 HERE

的解释方式

【问题讨论】:

  • 发布无效的UnityWebRequest 代码
  • 我做到了,但对我来说,这不是UnityWebRequest 的问题。我正在尝试通过“等待”代码而不将其重定向到链接或者在这种情况下,我丑陋/绝望的 PHP 脚本,找到一种从 Outlook 登录后获取授权代码的方法
  • 也许我忘记了有关 redirect_uri 的可能性,我只是不明白如何为桌面应用程序设置它
  • 也许我不明白你的问题。令牌不应该存储在json.access_token 变量中吗?
  • 是的,但我的问题是如何获取我需要的授权码来获取访问令牌而不重定向到网站。

标签: c# unity3d oauth-2.0 access-token outlook-api


【解决方案1】:

您需要使用环回重定向 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

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

【讨论】:

    猜你喜欢
    • 2018-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-01
    • 2016-04-13
    • 2017-05-28
    • 2021-09-05
    • 1970-01-01
    相关资源
    最近更新 更多