gpt4 book ai didi

java - 在 Twitter4j 中检索 OEmbed 对象时出现问题

转载 作者:行者123 更新时间:2023-12-02 10:53:53 25 4
gpt4 key购买 nike

我正在尝试从 Twitter4j 中的 Status 对象获取 OEmbed 对象,然后获取要传递到网页的 html。现在我只是将它放在一个主要方法中进行测试。

public static void main(String[] args) {
ConfigurationBuilder cf = new ConfigurationBuilder();

cf.setDebugEnabled(true)
.setOAuthConsumerKey("")
.setOAuthConsumerSecret("")
.setOAuthAccessToken("")
.setOAuthAccessTokenSecret("")
.setJSONStoreEnabled(true);

TwitterFactory tf = new TwitterFactory(cf.build());
Twitter twitter = tf.getInstance();

Query query = new Query("hello world");
query.setCount(5);

try {
QueryResult result = twitter.search(query);
List<Status> tweets = result.getTweets();
String jsonString = TwitterObjectFactory.getRawJSON(tweets.get(0));
System.out.println(jsonString);
OEmbed embed = TwitterObjectFactory.createOEmbed(jsonString);
System.out.println(embed.getHtml());
} catch(TwitterException e) {
e.printStackTrace();
}
}

凭据已被删除,但它们在程序中是正确的。控制台输出如下。

{"metadata":{"result_type":"recent","iso_language_code":"en"},"in_reply_to_status_id_str":null,"in_reply_to_status_id":null,"coordinates":null,"created_at":"Mon Sep 05 01:33:28 +0000 2016","truncated":false,"in_reply_to_user_id_str":null,"source":"<a href=\"http://twitter.com/download/iphone\" rel=\"nofollow\">Twitter for iPhone<\/a>","retweet_count":0,"retweeted":false,"geo":null,"in_reply_to_screen_name":null,"is_quote_status":false,"entities":{"urls":[],"hashtags":[],"user_mentions":[],"symbols":[]},"id_str":"772608554834341888","in_reply_to_user_id":null,"favorite_count":0,"id":772608554834341888,"text":"Neville is the most adorable boy in the world","place":{"country_code":"US","country":"United States","contained_within":[],"full_name":"Siloam Springs, AR","bounding_box":{"coordinates":[[[-94.569269,36.15253],[-94.487657,36.15253],[-94.487657,36.2142221],[-94.569269,36.2142221]]],"type":"Polygon"},"place_type":"city","name":"Siloam Springs","attributes":{},"id":"86abfc720d360e37","url":"https://api.twitter.com/1.1/geo/id/86abfc720d360e37.json"},"contributors":null,"lang":"en","user":{"utc_offset":null,"friends_count":258,"profile_image_url_https":"https://pbs.twimg.com/profile_images/767399989085483008/Xm0kzU-V_normal.jpg","listed_count":5,"profile_background_image_url":"http://abs.twimg.com/images/themes/theme1/bg.png","default_profile_image":false,"favourites_count":13032,"description":"Jesus. Dreams. World Domination Schemes.","created_at":"Wed Feb 26 23:03:16 +0000 2014","is_translator":false,"profile_background_image_url_https":"https://abs.twimg.com/images/themes/theme1/bg.png","protected":false,"screen_name":"hello_lovelyys","id_str":"2363303335","profile_link_color":"0084B4","is_translation_enabled":false,"id":2363303335,"geo_enabled":true,"profile_background_color":"C0DEED","lang":"en","has_extended_profile":false,"profile_sidebar_border_color":"C0DEED","profile_text_color":"333333","verified":false,"profile_image_url":"http://pbs.twimg.com/profile_images/767399989085483008/Xm0kzU-V_normal.jpg","time_zone":null,"url":null,"contributors_enabled":false,"profile_background_tile":false,"profile_banner_url":"https://pbs.twimg.com/profile_banners/2363303335/1471797391","entities":{"description":{"urls":[]}},"statuses_count":2760,"follow_request_sent":false,"followers_count":167,"profile_use_background_image":true,"default_profile":true,"following":false,"name":"a stray moonbeam","location":"Strolling through Mordor","profile_sidebar_fill_color":"DDEEF6","notifications":false},"favorited":false}

我确实得到了原始 JSON 字符串,但由于某种原因 createOEmbed 方法不喜欢它。堆栈跟踪如下。

JSONObject["html"] not found.
Relevant discussions can be found on the Internet at:
http://www.google.co.jp/search?q=e6edd861 or
http://www.google.co.jp/search?q=0003ff21
TwitterException{exceptionCode=[e6edd861-0003ff21 7fe5ced0-0f00c8c2], statusCode=-1, message=null, code=-1, retryAfter=-1, rateLimitStatus=null, version=4.0.4}
at twitter4j.OEmbedJSONImpl.init(OEmbedJSONImpl.java:64)
at twitter4j.OEmbedJSONImpl.<init>(OEmbedJSONImpl.java:46)
at twitter4j.TwitterObjectFactory.createOEmbed(TwitterObjectFactory.java:271)
at com.omnom.connections.TwitterConn.main(TwitterConn.java:53)
Caused by: twitter4j.JSONException: JSONObject["html"] not found.
at twitter4j.JSONObject.get(JSONObject.java:391)
at twitter4j.JSONObject.getString(JSONObject.java:505)
at twitter4j.OEmbedJSONImpl.init(OEmbedJSONImpl.java:51)
... 3 more

感谢您的阅读!

最佳答案

在尝试获取推文的 html 之前,您必须使用 OEmbedRequest 对象。

示例:

String url = ""

//gets url from id of tweet

OEmbedRequest oEmbedRequest = new OEmbedRequest("tweet id goes here", url);

//embeds into html code to be viewed

OEmbed embed = twitter.getOEmbed(oEmbedRequest);

//html result code

System.out.print(embed.getHtml());

关于java - 在 Twitter4j 中检索 OEmbed 对象时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39322957/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com