简体   繁体   English

在appengine中使用twitter4j的正确方法

[英]Right way to use twitter4j in appengine

I am practicing making a web app which tries to read the user's twitter profile, display his/her friends and show his/her picture. 我正在练习制作一个网络应用程序,试图阅读用户的推特个人资料,显示他/她的朋友并显示他/她的照片。 The code example at the Twitter4j website goes: Twitter4j网站上的代码示例如下:

public static void main(String args[]) thrwos Exception{
   Twitter twitter = new Twitter();
   twitter.setOAuthConsumer("[consumer key]", "[consumer secret]");
   RequestToken requestToken = twitter.getOAuthRequestToken();
   AccessToken accessToken = null;
   BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
   while (null == accessToken) {
        System.out.println("Open the following URL 
           and grant access to your account:");
        System.out.println(requestToken.getAuthorizationURL());
        System.out.print("Hit enter when it's done.[Enter]:");
        br.readLine();
        try{
          accessToken = requestToken.getAccessToken();
        } catch (TwitterException te) {
        if(401 == te.getStatusCode()){
        System.out.println("Unable to get the access token.");
        }else{
         te.printStackTrace();
       }
  }
 }
}

What I do is in a servlet, let's name it IndexServlet I have the following code: 我所做的是在一个servlet中,我们将它命名为IndexServlet我有以下代码:

   Twitter twitter = new Twitter();
   twitter.setOAuthConsumer("[consumer key]", "[consumer secret]");
   RequestToken requestToken = twitter.getOAuthRequestToken();
   String authUrl = requestToken.getAuthorizationURL()

and then I pass the authUrl to a jsp so the user can click on it. 然后我将authUrl传递给jsp,以便用户可以单击它。 That works so far but after that I am already stuck! 这工作到目前为止,但在那之后我已经卡住了! :) :)

I am not so sure where to set the callback URL. 我不确定在哪里设置回调网址。 Should I set it to the same servlet? 我应该将它设置为相同的servlet吗? The problem with that is I should have the same twitter and requestToken variables just like the first code with main method so I can do this: 问题是我应该使用相同的twitter和requestToken变量,就像使用main方法的第一个代码一样,所以我可以这样做:

accessToken = requestToken.getAccessToken();
Status status = twitter.updateStatus(some argument here);

But setting the callback url to the IndexServlet calls the code that setups the twitter consumer. 但是将回调URL设置为IndexServlet会调用设置twitter使用者的代码。 I could probably make a way around it using a flag like 我可以用一个标志来绕过它

if (already setup) {
   accessToken = requestToken.getAccessToken();
   Status status = twitter.updateStatus(some argument here);
} 

but then I think that's messy. 但后来我觉得这很麻烦。

What I've tried is (using a second servlet which is the callback url) 我试过的是(使用第二个servlet,它是回调网址)

  1. I tried storing the twitter and requestToken variables in the session so I can access it when twitter redirects to the callback url but apparently it's not allowed. 我尝试在会话中存储twitter和requestToken变量,这样我就可以在twitter重定向到回调网址时访问它,但显然它是不允许的。

  2. I also tried saving both twitter and request token to the datastore but the latter does not allow RequestToken types to be stored. 我还尝试将twitter和请求令牌保存到数据存储区,但后者不允许存储RequestToken类型。

So apparently all have failed. 显然所有人都失败了。 Was wondering about the right way to do this. 想知道正确的方法来做到这一点。 Thanks! 谢谢! :) :)

I worked out a hack to use Twitter4j + Oauth in google app engine. 我在谷歌应用引擎中使用了Twitter4j + Oauth。 I blogged about it here . 我在这里写博客。

Although this code works, it might not be the right way to use Twitter4j and Oauth (it does work on appengine though). 尽管此代码有效,但它可能不是使用Twitter4j和Oauth的正确方法(尽管它确实适用于appengine)。 It's not optimized, it's definitely not the best way and, for the sake of brevity, it's actually riddled with bad practices! 它没有经过优化,它绝对不是最好的方式,为了简洁起见,它实际上充斥着不良做法! In my real app though I have refactored those code smells already. 在我的真实应用程序中虽然我已经重构了那些代码味道。

Another thing, I have yet to address is why Twitter's icon shows up after it redirects to the to callback url. 另一件事,我还没有解决为什么Twitter的图标在重定向到回调网址后显示的原因。 I intend to work on that soon as well as optimizing the method. 我打算尽快开展这项工作并优化方法。 Comments and suggestions are highly welcome! 欢迎提出意见和建议!

You can store the data in the session . 您可以将数据存储在会话中 Google App Engine supports this, but you need to enable it . Google App Engine支持此功能,但您需要启用它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM