当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript Dropbox.getAuthenticationUrl方法代码示例

本文整理汇总了TypeScript中dropbox.Dropbox.getAuthenticationUrl方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Dropbox.getAuthenticationUrl方法的具体用法?TypeScript Dropbox.getAuthenticationUrl怎么用?TypeScript Dropbox.getAuthenticationUrl使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在dropbox.Dropbox的用法示例。


在下文中一共展示了Dropbox.getAuthenticationUrl方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。

示例1: authenticate

function authenticate() {
  const client = new Dropbox({ clientId: CLIENT_ID });
  const authUrl = client.getAuthenticationUrl("http://localhost:3030/auth");
  const app = express();
  app.use(jsonBodyParser());
  let server: Server;

  app.get('/auth', function(req, res) {
    res.status(200);
    // Write code that POST's code to server from client.
    res.write(Buffer.from(`<!DOCTYPE html>
<html>
  <head>
    <title>BrowserFS Dropbox Authentication</title>
  </head>
  <body>
    <h1 id="status">Sending access token to BrowserFS...</h1>

    <p id="details">Please wait one moment...</p>

    <script type="text/javascript">
      (function() {
        var status = document.getElementById('status');
        var detail = document.getElementById('detail');
        // Send full URL, including #, to server.
        var xhr = new XMLHttpRequest();
        xhr.onload = function() {
          var statusCode = xhr.status;
          var statusText = xhr.statusText;
          if (statusCode >= 200 && statusCode < 300) {
            document.title = "Success!";
            status.innerText = "Success!";
            details.innerText = xhr.responseText;
          } else {
            document.title = "Failed";
            status.innerText = "Failed";
            details.innerText = xhr.responseText;
          }
        };
        xhr.open('POST', 'http://localhost:3030/authurl');
        xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
        xhr.send(JSON.stringify({url: window.location.href}));
      })();
    </script>
  </body>
</html>`, 'utf8'));
    res.end();
  });

  app.post('/authurl', function(req, res) {
    if (!req.body || !req.body.url) {
      console.error(`Unable to retrieve Dropbox access token.`);
      res.status(400);
      res.send();
      server.close(() => process.exit(1));
    } else {
      const rawUrl = req.body.url;
      // Dropbox URLs use query params, but use # instead of ? to denote them.
      // Switch to '?' so the url package can parse them.
      const url = parseUrl(rawUrl.replace("#", "?"), true);
      console.log(req.url);
      console.log(url);
      const token = url.query['access_token'];
      if (token) {
        res.status(200);
        writeFileSync(tokenPath, Buffer.from(`{ "accessToken": "${token}" }`, 'utf8'));
        const successMsg = `Successfully saved Dropbox token to ${tokenPath}. You may now close your browser.`;
        console.log(successMsg);
        res.write(Buffer.from(successMsg, 'utf8'));
      } else {
        res.status(400);
        const failureMsg = `Unable to retrieve Dropbox token. Please try again.`;
        console.log(failureMsg);
        res.write(Buffer.from(failureMsg, 'utf8'));
      }
      res.end();
      setTimeout(() => {
        server.close(function() {
          // Exit w/ error if failed to get token.
          process.exit(token ? 0 : 1);
        });
      }, 500);
    }
  });

  server = app.listen(3030, function() {
    console.log(`Navigate to ${authUrl} and log in to Dropbox.`);
  });
}
开发者ID:jvilk,项目名称:BrowserFS,代码行数:89,代码来源:get_db_credentials.ts


注:本文中的dropbox.Dropbox.getAuthenticationUrl方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。