2014-05-20 57 views
0

我有下面的圖片如何嵌入最新的鳴叫在Sitecore的6.5

enter image description here

我怎樣才能實現這個功能給在Sitecore的6.5項目中嵌入最新的鳴叫。

感謝

+4

你嘗試過什麼? –

+0

看起來像我的作業 –

+0

Sitecore作業?真?我會喜歡一些! hahaha –

回答

1

正如你將它添加到任何網站......爲它創建一個組件,並使用Twitter的API類似這樣的例子:http://jsfiddle.net/abenlumley/xRwam/4/

/********************************************************************* 
    #### Twitter Post Fetcher! #### 
    Coded by Jason Mayes 2013. 
    www.jasonmayes.com 
    Please keep this disclaimer with my code if you use it. Thanks. :-) 
    Got feedback or questions, ask here: http://goo.gl/JinwJ 

    Ammended by Ben Lumley and djb31st 2013 
    www.dijitul.com 
    Ammended to display latest tweet only with links 
********************************************************************/ 
var twitterFetcher=function(){var d=null;return{fetch:function(a,b){d=b;var c=document.createElement("script");c.type="text/javascript";c.src="http://cdn.syndication.twimg.com/widgets/timelines/"+a+"?&lang=en&callback=twitterFetcher.callback&suppress_response_codes=true&rnd="+Math.random();document.getElementsByTagName("head")[0].appendChild(c)},callback:function(a){var b=document.createElement("div");b.innerHTML=a.body;a=b.getElementsByClassName("e-entry-title");d(a)}}}(); 

/* 
* ### HOW TO USE: ### 
* Create an ID: 
* Go to www.twitter.com and sign in as normal, go to your settings page. 
* Go to "Widgets" on the left hand side. 
* Create a new widget for "user timeline". Feel free to check "exclude replies" 
* if you dont want replies in results. 
* Now go back to settings page, and then go back to widgets page, you should 
* see the widget you just created. Click edit. 
* Now look at the URL in your web browser, you will see a long number like this: 
* 345735908357048478 
* Use this as your ID below instead! 
*/ 

twitterFetcher.fetch('345190342812909568', function(tweets){ 
    // Do what you want with your tweets here! For example: 
    var x = tweets.length; 
    var n = 0; 
    var element = document.getElementById('tweets'); 
    var html = '<ul>'; 
    if (tweets[n].innerHTML) { 
     html += '<li>' + tweets[n].innerHTML + '</li>'; 
    } else { 
     html += '<li>' + tweets[n].textContent + '</li>'; 
    } 
    n++; 
    html += '</ul>'; 
    element.innerHTML = html; 
}); 
+0

感謝您的回覆...但在這裏我只得到text.I必須顯示'日期'還...如何獲得'日期'特別推文... – Sukhjeevan

+1

@Sukhi看看https:// dev .twitter.com/docs您可以找到有關使用twitter API的所有信息。 – IvanL

+0

此示例使用#標籤獲取最新推文,但是如何通過用戶名獲取推文? – Sukhjeevan

0

正如@IvanL說,你只需要創建一個子佈局,並像通常那樣添加標記/ JS /等。下面,我介紹一個易於使用的庫,它可以幫助您通過Twitter的API獲取您的推文,並且還可以使用jQuery插件來簡化呈現方式。您需要做的就是連接庫,進行必要的C#調用,然後使用jQuery插件來幫助您使用指定的標記樣式來呈現Tweets。

如下所述,請注意,我最初編寫了用於與Sitecore 6.5環境集成的庫和jQuery插件,並使它們足夠靈活以適用於任何解決方案。

獲取和渲染鳴叫

我關於Twitter的API創建的C#庫一年前,命名爲TweetNET。它具有MSDN樣式文檔,並且我可以將它集成到.NET應用程序中,並且我使用的第一個生產站點是Sitecore 6.5站點。文檔和示例非常全面,但如果您有任何問題,請隨時通知我。

至於從Twitter獲取推文後的實際顯示情況,我還有另一個回購,Twitter Feed,這是一個jQuery插件,旨在簡化呈現推文。這兩個項目都包含TweetNET的使用示例,Twitter Feed項目還包含其呼叫示例,因此這將成爲您的一站式商店。


TweetNET - 最新的鳴叫呼叫

TweetNET減少了你需要爲了得到一個指定句柄的最新推文下面的代碼:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using TweetNET.OAuth; 
using TweetNET.Requests.Timelines.Statuses; 
using System.Net; 
using System.IO; 

public partial class _Default : System.Web.UI.Page { 
    protected void Page_Load(object sender, EventArgs e) { 
     var consumerKey = "your consumerKey"; 
     var consumerSecret = "your consumerSecret"; 
     var oAuthToken = "your oAuthToken"; 
     var oAuthTokenSecret = "your oAuthTokenSecret"; 

     var twitterHandle = "your twitter handle"; 

     var tokens = new SecurityTokens(consumerKey, consumerSecret, oAuthToken, oAuthTokenSecret); 

     var utGETRequest = new UserTimelineRequest(tokens); 
     utGETRequest.Screen_Name = twitterHandle; 

     var request = utGETRequest.BuildRequest(); 
     WebResponse response = utGETRequest.SendRequest(request); 
     string responseData = new StreamReader(response.GetResponseStream()).ReadToEnd(); 
    } 
} 


微博飼料 - 渲染推文

$(document).ready(function() { 
     $("#feedTarget").twitterFeed({ 
       count: 4, 
       rawData: yourRawJSONData, 
       prepend: "<div class='tweetWrapper'>", 
       append: "</div>", 
       tweetBodyClass: "tweetBody tweetText", 
       date: { prepend: "<div>", append: " - ", order: 3, cssClass: "tweetDate" }, 
       retweet: { show: false }, 
       favorite: { prepend: " - ", order: 0, append: "</div>" }, 
       callbackOnEach: true, 
       callback: function() { 
       $(this).find(".tweetBody").myCallbackOnEachTweet(); 
      } 
      }); 
    }); 
}); 
2

你好你可以這樣做看下面的代碼。我在這裏粘貼代碼來進行單個子佈局。請根據您的要求更新一些令牌。這段代碼將返回一個Json,你可以在JQuery中獲得這個json。

代碼 - ----------------

公共部分類LatestTweets:BaseSublayout { SiteItem objSiteItem = SiteItem.GetSiteRoot(); protected void Page_Load(object sender,EventArgs e) if(objSiteItem!= null) { hdJsonData.Value = GetTweets(); frLatestTweets.Item = objSiteItem; frLatestTweets.Item = objSiteItem; frFollowUsLink.Item = objSiteItem; ltFollowUs.Text = Sitecore.Globalization.Translate.Text(Constants.FOLLOW_US); ltTweetUs.Text = Sitecore.Globalization.Translate.Text(Constants.TWEET_US); }}

public string GetTweets() 
    { 

     // oauth application keys 
     var oauth_token = objSiteItem.AccessToken.Rendered; 
     var oauth_token_secret = objSiteItem.AccessTokenSecret.Rendered; 
     var oauth_consumer_key = objSiteItem.ConsumerKey.Rendered; 
     var oauth_consumer_secret = objSiteItem.ConsumerSecret.Rendered; 
     var screen_name = objSiteItem.TwitterUser.Rendered; 
     // oauth implementation details 
     var oauth_version = "1.0"; 
     var oauth_signature_method = "HMAC-SHA1"; 

     // unique request details 
     var oauth_nonce = Convert.ToBase64String(
      new ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString())); 

     var timeSpan = DateTime.UtcNow 
      - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc); 

     var oauth_timestamp = Convert.ToInt64(timeSpan.TotalSeconds).ToString(); 

     // message api details 
     var status = "Updating status via REST API if this works"; 

     var resource_url = "https://api.twitter.com/1.1/statuses/user_timeline.json"; 

     // create oauth signature 
     var baseFormat = "oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}" + 
         "&oauth_timestamp={3}&oauth_token={4}&oauth_version={5}&screen_name={6}"; 

     var baseString = string.Format(baseFormat, 
             oauth_consumer_key, 
             oauth_nonce, 
             oauth_signature_method, 
             oauth_timestamp, 
             oauth_token, 
             oauth_version, 
             Uri.EscapeDataString(screen_name) 
            ); 

     baseString = string.Concat("GET&", Uri.EscapeDataString(resource_url), "&", Uri.EscapeDataString(baseString)); 

     var compositeKey = string.Concat(Uri.EscapeDataString(oauth_consumer_secret), 
           "&", Uri.EscapeDataString(oauth_token_secret)); 

     string oauth_signature; 

     using (HMACSHA1 hasher = new HMACSHA1(ASCIIEncoding.ASCII.GetBytes(compositeKey))) 
     { 
      oauth_signature = Convert.ToBase64String(hasher.ComputeHash(ASCIIEncoding.ASCII.GetBytes(baseString))); 
     } 

     // create the request header 
     var headerFormat = "OAuth oauth_nonce=\"{0}\", oauth_signature_method=\"{1}\", " + 
          "oauth_timestamp=\"{2}\", oauth_consumer_key=\"{3}\", " + 
          "oauth_token=\"{4}\", oauth_signature=\"{5}\", " + 
          "oauth_version=\"{6}\""; 

     var authHeader = string.Format(headerFormat, 
           Uri.EscapeDataString(oauth_nonce), 
           Uri.EscapeDataString(oauth_signature_method), 
           Uri.EscapeDataString(oauth_timestamp), 
           Uri.EscapeDataString(oauth_consumer_key), 
           Uri.EscapeDataString(oauth_token), 
           Uri.EscapeDataString(oauth_signature), 
           Uri.EscapeDataString(oauth_version) 
         ); 

     // make the request 
     ServicePointManager.Expect100Continue = false; 

     var postBody = "screen_name=" + Uri.EscapeDataString(screen_name);// 
     resource_url += "?" + postBody; 
     HttpWebRequest request = (HttpWebRequest)WebRequest.Create(resource_url); 
     request.Headers.Add("Authorization", authHeader); 
     request.Method = "GET"; 
     request.ContentType = "application/x-www-form-urlencoded"; 

     WebResponse response = request.GetResponse(); 
     string responseData = new StreamReader(response.GetResponseStream()).ReadToEnd(); 
     return responseData; 
    } 
} 
+0

感謝您的回覆......如果我們在後面的代碼中實現此功能,這也是有幫助的... – Sukhjeevan

0

,以獲得最新的tweet閱讀以下網址

https://umerpasha.wordpress.com/2013/06/13/c-code-to-get-latest-tweets-using-twitter-api-1-1/