2012-12-02 19 views
2

我正在嘗試爲本地項目使用骨幹網,這個項目永遠不會看到真正的Web服務器,並且通過AJAX與Yelp API進行交互。 Yelp API要求我們使用oauth進行身份驗證,並給出sample code我已經建立了自己的代碼。當我使用示例代碼時,我不會遇到Cross-Origin或其他任何問題。但是,當我關閉瀏覽器安全選項時,我只收到400個錯誤響應。Backbone覆蓋Oauth的collection.sync

我曾試圖覆蓋如下所述提取方法:

fetch: function(options) { 
    var accessor, message, parameterMap, parameters; 
    if (!options) { 
    options = {}; 
    } 
    accessor = { 
    consumerSecret: LOAF.auth.conserumerSecret, 
    tokenSecret: LOAF.auth.accessTokenSecret 
    }; 
    parameters = []; 
    parameters.push(['oauth_consumer_key', LOAF.auth.consumerKey]); 
    parameters.push(['oauth_consumer_secret', LOAF.auth.consumerSecret]); 
    parameters.push(['oauth_token', LOAF.auth.accessToken]); 
    parameters.push(['oauth_signature_method', 'HMAC-SHA1']); 
    parameters.push(['location', "New York City"]); 
    message = { 
    'action': this.url, 
    'method': 'GET', 
    'parameters': parameters 
    }; 
    OAuth.setTimestampAndNonce(message); 
    OAuth.SignatureMethod.sign(message, accessor); 
    parameterMap = OAuth.getParameterMap(message.parameters); 
    parameterMap.oauth_signature = OAuth.percentEncode(parameterMap.oauth_signature); 
    options.url = this.url; 
    options.data = parameterMap; 
    options.cache = true; 
    options.dataType = 'json'; 
    options.success = this.onResponse; 
    console.log("Attempting"); 
    console.log(options); 
    return Backbone.Model.prototype.fetch.apply(this, options); 
}, 

但這產生400響應。我有一種感覺,因爲我沒有正確地進行AJAX調用,因爲主幹對我來說大部分都是這樣做的,可能會覆蓋我設置的一些選項。我認爲我需要做的是覆蓋此集合的「同步」方法,而不是僅僅處理OAuth並自己解析響應。有一個更好的方法嗎?

回答

1

我不確定這是否是正確的方式來做事,但它是這樣工作的,所以這是我的解決方案。我重寫抓取並從不調用默認的抓取方法。相反,我使用fetch方法進行ajax調用,並使用onResponse方法處理響應,爲響應創建模型(Yelp企業)。希望這可以幫助任何處於相同位置的人。

LOAF.YelpList = Backbone.Collection.extend({ 
    model: LOAF.Business, 
    url: 'http://api.yelp.com/v2/search?', 
    fetch: function(options) { 
    var accessor, message, parameterMap, parameters; 
    if (!options) { 
     options = {}; 
    } 
    accessor = { 
     consumerSecret: LOAF.auth.consumerSecret, 
     tokenSecret: LOAF.auth.accessTokenSecret 
    }; 
    parameters = []; 
    parameters.push(['callback', 'cb']); 
    parameters.push(['oauth_consumer_key', LOAF.auth.consumerKey]); 
    parameters.push(['oauth_consumer_secret', LOAF.auth.consumerSecret]); 
    parameters.push(['oauth_token', LOAF.auth.accessToken]); 
    parameters.push(['oauth_signature_method', 'HMAC-SHA1']); 
    parameters.push(['location', "New York City"]); 
    message = { 
     'action': this.url, 
     'method': 'GET', 
     'parameters': parameters 
    }; 
    OAuth.setTimestampAndNonce(message); 
    OAuth.SignatureMethod.sign(message, accessor); 
    parameterMap = OAuth.getParameterMap(message.parameters); 
    parameterMap.oauth_signature = OAuth.percentEncode(parameterMap.oauth_signature); 
    options.url = this.url; 
    options.data = parameterMap; 
    options.cache = true; 
    options.dataType = 'jsonp'; 
    options.jsonpCallback = 'cb'; 
    options.success = this._onResponse; 
    options.context = this; 
    return $.ajax(options); 
    }, 
    _onResponse: function(data, textStats, xhr) { 
    debugger; 
    var _this = this; 
    return _.each(data.businesses, function(business) { 
     var busModel; 
     if (!_this.get(business.id)) { 
     busModel = new LOAF.Business(business); 
     return _this.add(busModel); 
     } 
    }); 
    } 
}); 

有兩點需要注意:

  1. 最初的代碼會工作,但consumerSecret被拼寫錯了,造成不正確的OAuth的電話。這就是我得到400錯誤的原因。
  2. 即使你解決了這個問題,也存在跨域調用的問題。切換到我使用的AJAX方法緩解了這個問題。但是,可能有一種方法可以解決這個更原生的Backbone問題。對於我的項目的時間框架,這是不行的。