如何等到Vimeo。accessToken方法完成。认为用问吗?

时间:2022-08-23 18:04:38

I'm writing a small app in node.js that interfaces with Vimeo's API. I installed the official Node SDK from vimeo using npm install vimeo-api. Everything is working fine, but now I want to change the program flow to be more "promise like". For example, I would like for my program to wait until it receives the OAuth access token returned by the Vimeo.accessToken method before it executes any new code. How can I acheive this?

我在节点上写一个小程序。与Vimeo的API接口。我使用npm安装vimeo-api从vimeo安装了官方的节点SDK。一切都很好,但是现在我想要改变程序流,使其更像“承诺”。例如,我希望我的程序等待直到它收到Vimeo返回的OAuth访问令牌。accessToken方法在执行任何新代码之前。我怎样才能把它弄好呢?

Here is some code driving the retrieval of the access token--very much taken from the examples on the SDK github page:

下面是一些驱动访问令牌检索的代码——非常取自SDK github页面上的示例:

var vmoapi = require('vimeo-api'),
    vmo = new vmoapi(VMO_CLIENT_ID, VMO_CLIENT_SECRET);
    .
    .
    .

function getAccessToken(code, redirectURI){

    vmo.accessToken(code, redirectURI, function(err, token){

        if (err) { 
            console.log("Vimeo API Error\n" + err);
            return;
        }

        if (token.access_token){

            console.log(nutil.inspect(token));
            vmo.access_token = token.access_token;
            user = token.user;
            userScopes = token.scope;

            return token;

        }

    })
}   

In node.js how can I do something like the following:

在节点。我如何做以下事情:

getAccessToken(myCode, "http://someplace.com").done(function(data){
     console.log("Access token" + nutil.inspect(data));
})

I thought maybe I would use Kris Kowal's Q library, but am not certain about how to go about implementing it so that my app flows in the manner that I would like.

我想我可能会使用Kris Kowal的Q库,但是我不确定如何实现它,这样我的应用就会以我喜欢的方式流动。

1 个解决方案

#1


0  

So after reading Q's documentation and some examples, here is what I implemented and it seems to be working!

所以,在阅读了Q的文档和一些示例之后,这里是我实现的,它似乎正在工作!

var vmoapi = require('vimeo-api'),
    vmo = new vmoapi(VMO_CLIENT_ID, VMO_CLIENT_SECRET),
    .
    .
    .
    deferred =  Q.deferred();

 .
 .
 .

 function getAccessToken(code, redirectURI) {

      vmo.accessToken(code, redirectURI, function(err, token) {

             if (err) {
                console.log("Vimeo API Error\b" + err);
                deferred.reject(new Error(err));
             }

             if (token.access_token){
                vmo.access_token = token.access_token;
                user = token.user;
                userScopes = token.scope;

                deferred.resolve(token);
             }
      })

      return deferred.promise;
 }

getAccessToken(parsedURL.query.code, vmoRedirectURI)
              .then(function(data){
                       console.log("Vimeo access token:\n" + nutil.inspect(data))
               });

The solution involves use the Q.deferred() object and then setting up the .reject and .resolve methods within the Vimeo.accessToken callback function. Then at the end of the function that calls the Vimeo.accessToken method, return the deferred.promise.

解决方案包括使用Q.deferred()对象,然后在Vimeo中设置.reject和.resolve方法。accessToken回调函数。然后在调用Vimeo的函数的末尾。使用accessToken方法,返回延迟。

#1


0  

So after reading Q's documentation and some examples, here is what I implemented and it seems to be working!

所以,在阅读了Q的文档和一些示例之后,这里是我实现的,它似乎正在工作!

var vmoapi = require('vimeo-api'),
    vmo = new vmoapi(VMO_CLIENT_ID, VMO_CLIENT_SECRET),
    .
    .
    .
    deferred =  Q.deferred();

 .
 .
 .

 function getAccessToken(code, redirectURI) {

      vmo.accessToken(code, redirectURI, function(err, token) {

             if (err) {
                console.log("Vimeo API Error\b" + err);
                deferred.reject(new Error(err));
             }

             if (token.access_token){
                vmo.access_token = token.access_token;
                user = token.user;
                userScopes = token.scope;

                deferred.resolve(token);
             }
      })

      return deferred.promise;
 }

getAccessToken(parsedURL.query.code, vmoRedirectURI)
              .then(function(data){
                       console.log("Vimeo access token:\n" + nutil.inspect(data))
               });

The solution involves use the Q.deferred() object and then setting up the .reject and .resolve methods within the Vimeo.accessToken callback function. Then at the end of the function that calls the Vimeo.accessToken method, return the deferred.promise.

解决方案包括使用Q.deferred()对象,然后在Vimeo中设置.reject和.resolve方法。accessToken回调函数。然后在调用Vimeo的函数的末尾。使用accessToken方法,返回延迟。