【问题标题】:"An item with the same key has already been added." while adding "/&output=embed"“已经添加了具有相同密钥的项目。”同时添加“/&output=embed”
【发布时间】:2016-03-14 19:25:15
【问题描述】:

使用 Evernote API 在 C# 中实现 MVC 应用程序。我正在使用AsyncOAuth.Evernote.Simple nuget 包。 Receiving and error of Refused to display in a frame because it set 'X-Frame-Options' to 'SAMEORIGIN',当尝试导航到触发 OAuth 进程的 URL 时。

我的代码周围有一个 iframe(无法更改)。执行代码后会产生错误:"An item with the same key has already been added". 第一次命中requestToken 时会出现此错误。

下面是我的EvernoteProviderController.cs

public class EvernoteProviderController : Controller
{



    // Initialize Oauth call, pulling values from web.config
   EvernoteAuthorizer EvernoteAuthorizer = new EvernoteAuthorizer(ConfigurationManager.AppSettings["Evernote.Url"] + "&output=embed", ConfigurationManager.AppSettings["Evernote.Key"], ConfigurationManager.AppSettings["Evernote.Secret"]);



    // This method makes the original call to Evernote to get a token so that the user can validate that they want to access this site.
    public ActionResult Authorize(bool reauth = false)
    {
        // Allow for reauth
        if (reauth)
            SessionHelper.Clear();

        // First of all, check to see if the user is already registered, in which case tell them that
        if (SessionHelper.EvernoteCredentials != null)
            return Redirect(Url.Action("AlreadyAuthorized"));

        // Evernote will redirect the user to this URL once they have authorized your application
        var callBackUrl = Request.Url.GetLeftPart(UriPartial.Authority) + Url.Action("ObtainTokenCredentials");

        // Generate a request token - this needs to be persisted till the callback
        var requestToken = EvernoteAuthorizer.GetRequestToken(callBackUrl);

        // Persist the token
        SessionHelper.RequestToken = requestToken;

        // Redirect the user to Evernote so they can authorize the app
        var callForwardUrl = EvernoteAuthorizer.BuildAuthorizeUrl(requestToken);
        return Redirect(callForwardUrl);
    }

    // This action is the callback that Evernote will redirect to after the call to Authorize above
    public ActionResult ObtainTokenCredentials(string oauth_verifier)
    {
        // Use the verifier to get all the user details we need and store them in EvernoteCredentials
        var credentials = EvernoteAuthorizer.ParseAccessToken(oauth_verifier, SessionHelper.RequestToken);
        if (credentials != null)
        {
            SessionHelper.EvernoteCredentials = credentials;
            return Redirect(Url.Action("Authorized"));
        }
        else
        {
            return Redirect(Url.Action("Unauthorized"));
        }
    }

    // Show the user if they are authorized
    public ActionResult Authorized()
    {
        return View(SessionHelper.EvernoteCredentials);
    }

    public ActionResult Unauthorized()
    {
        return View();
    }


    //Redirects user if already authorized, then dump out the EvernoteCredentials object
    public ActionResult AlreadyAuthorized()
    {
        return View(SessionHelper.EvernoteCredentials);
    }

    public ActionResult Settings()
    {
        return View();
    }

}

以前有没有人遇到过 iframe 的问题,或者知道我应该往哪个方向发展?我正在尝试嵌入我的 URL 端点,以便绕过 iframe 错误。

【问题讨论】:

    标签: c# asp.net-mvc iframe oauth evernote


    【解决方案1】:

    解决了错误。

    一点背景故事:

    此应用程序的目的是提供 OAuth 页面,用户可以在其中注册,该页面将生成 AuthTokenNotebookURL,(两者都需要 Evernote API 来读取/写入 Notes - 这是Evernote 的对象)。

    之前的行为(在我更改之前)是当用户点击链接时 - 他们将被重定向(在同一窗口中)到 Evernote OAuth 页面。

    这给我带来了问题,因为我的代码有另一个包装器 (iframe)。所以在非技术方面,我在iframe 中有一个iframe 和一个iframe

    解决方法

    创建了一个 JavaScript 代码,该代码将添加一个 click 事件侦听器,然后使用 window.open 创建一个弹出窗口。

    $("#btnStart").click(function () {
    
        myWindow = window.open(baseUrl + "/EvernoteProvider/Authorize", '_blank', 'width=500,height=500, scrollbars=no,resizable=no');
        myWindow.focus();
    
    });
    

    【讨论】:

      猜你喜欢
      • 2011-01-27
      • 2021-08-02
      • 2011-03-01
      • 2017-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多