开发者

How to use bit.ly for Twitter

I'm developing a J2EE website with spring framework. I want my website to share with Twitter but I couldn't succeed using bit.ly API. the function makes bit.ly link but in Twitter's share page I only see the full link. How can I send the bit.ly link to Twitter?

The bit.ly response which I get from firebug:

BitlyCB.getBitlyUrl({"errorCode": 0, "errorMessage": "", "results": {"http://twitter.com/home?status=http://www.google.com": {"userHash": "dodUFu", "hash": "9KnUl2", "shortUrl": "http://bit.ly/dodUFu", "shortCNAMEUrl": "http://bit.ly/dodUFu", "shortKeywordUrl": ""}}, "statusCode": "OK"})

If you try http://twitter.com/home?status=http://www.google.com you can understand me.

My code is below:

<script typ开发者_如何学Ce="text/javascript" charset="utf-8"
    src="http://bit.ly/javascript-api.js?version=latest&amp;login=mylogin&amp;apiKey=mykey"></script>
<a class="_ffShare_"
onclick="onlyShortenUrl('http://twitter.com/home?status=http://mypage');">
<img src="http://yakup-laptop:8080/images/theme/default/twitter.png"></img>
</a>

    function onlyShortenUrl(longUrl){
    //single shortener
    BitlyCB.getBitlyUrl = function(data) {
        var shortUrl = extractShortUrl(data);
        window.open(shortUrl,'_blank');
        return shortUrl;
    }
    return BitlyClient.call('shorten', {'longUrl': longUrl}, 'BitlyCB.getBitlyUrl');
    }

    function extractShortUrl(data){    
    //bitly util method probably not useful standalone
       var shortUrl = '';
       var first_result; 
       // Results are keyed by longUrl, so we need to grab the first one.
       for (var r in data.results) {
         first_result = data.results[r]; break;
       }
       for (var key in first_result) {
         shortUrl = r ;
       } 
       return shortUrl;
   }


Try changing:

for (var key in first_result) {
         shortUrl = r ;
}

to

shortUrl = first_result.shortUrl;

If that doesn't work, please include the output (at that same point) of first_result:

console.log(first_result);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜