|
| 1 | +package com.baeldung.scribejava.controller; |
| 2 | + |
| 3 | +import com.baeldung.scribejava.service.TwitterService; |
| 4 | +import com.github.scribejava.core.model.*; |
| 5 | +import org.springframework.beans.factory.annotation.Autowired; |
| 6 | +import org.springframework.web.bind.annotation.GetMapping; |
| 7 | +import org.springframework.web.bind.annotation.RestController; |
| 8 | + |
| 9 | +import javax.servlet.http.HttpServletResponse; |
| 10 | +import java.io.IOException; |
| 11 | +import java.util.Scanner; |
| 12 | +import java.util.concurrent.ExecutionException; |
| 13 | + |
| 14 | +@RestController |
| 15 | +public class TwitterController { |
| 16 | + |
| 17 | + @Autowired |
| 18 | + private TwitterService service; |
| 19 | + |
| 20 | + |
| 21 | + @GetMapping(value ="/me/twitter") |
| 22 | + public String me(HttpServletResponse servletResponse){ |
| 23 | + try { |
| 24 | + OAuth1RequestToken requestToken = service.getService().getRequestToken(); |
| 25 | + |
| 26 | + String auth = service.getService().getAuthorizationUrl(requestToken); |
| 27 | + |
| 28 | + Runtime runtime = Runtime.getRuntime(); |
| 29 | + try { |
| 30 | + runtime.exec("rundll32 url.dll,FileProtocolHandler " + auth); |
| 31 | + } catch (IOException e) { |
| 32 | + servletResponse.setStatus(HttpServletResponse.SC_BAD_REQUEST); |
| 33 | + return null; |
| 34 | + } |
| 35 | + |
| 36 | + System.out.println("Insert twitter code:"); |
| 37 | + Scanner in = new Scanner(System.in); |
| 38 | + |
| 39 | + String oauthverifier = in.nextLine(); |
| 40 | + |
| 41 | + final OAuth1AccessToken accessToken = service.getService().getAccessToken(requestToken,oauthverifier); |
| 42 | + |
| 43 | + OAuthRequest request = new OAuthRequest(Verb.GET, "https://api.twitter.com/1.1/account/verify_credentials.json"); |
| 44 | + service.getService().signRequest(accessToken, request); |
| 45 | + Response response = service.getService().execute(request); |
| 46 | + return response.getBody(); |
| 47 | + |
| 48 | + } catch (IOException | InterruptedException | ExecutionException e) { |
| 49 | + servletResponse.setStatus(HttpServletResponse.SC_BAD_REQUEST); |
| 50 | + } |
| 51 | + |
| 52 | + return null; |
| 53 | + } |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | +} |
0 commit comments