Springboot Twitter Binding API Authorization - java

i want to make my springboot application can connect into twitter through API using my secret and client key. Here it is my code
package com.backend.siap.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.social.twitter.api.Tweet;
import org.springframework.social.twitter.api.Twitter;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
#RestController
public class TestTwitter {
#Autowired
private Twitter twitter;
#RequestMapping(value="twitter/{hashTag}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public List<Tweet> getTweets (#PathVariable final String hashTag)
{
return twitter.searchOperations().search(hashTag, 5).getTweets();
}
#RequestMapping(value="timeline", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public List<Tweet> getTimeline()
{
return twitter.timelineOperations().getHomeTimeline();
}
}
if i call getTweet method controller it return json that contain some tweet that i search using hashtag. so it work
but if i call getTimeline method controller it return something like this
org.springframework.social.MissingAuthorizationException: Authorization is required for the operation, but the API binding was created without authorization.
I also have added my secret and client code in my application properties.
how to solve that problem? thanks

Related

Cloud Function with Java and Springboot

I'm pretty new to this stuff, but I need it for university. So maybe someone can help me please?
I've made a little Java Application with Springboot to handle a simple ToDo-List:
package coco.ToDo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
#RestController
public class ToDoListController {
private final ToDoListService toDoListService;
#Autowired
public ToDoListController(ToDoListService toDoListService) {
this.toDoListService = toDoListService;
}
#RequestMapping(value = "/ToDoList")
public List<ToDo> getToDoList() {
return toDoListService.getToDoList();
}
#RequestMapping("/ToDoList/add")
public void addToDo(#RequestParam("ToDo") String toDo){
toDoListService.addToDo(toDo);
}
}
I would like to run it on gcloud with Cloud Functions.
But i haven't figured it out yet.
Cloud Functions is designed to handle only a function, only ONE entry point. If you have several entry point (request mapping) you must host a webserver on App Engine or on Cloud Run.

How can I resolve this issue, I am unable to access my controller class methods from Postman or Browser?

I am running a simple Spring boot application, In console its running successfully. But when I am trying by postman it gives this error message.
{
"timestamp": "2020-07-26T04:22:15.626+00:00",
"status": 404,
"error": "Not Found",
"message": "",
"path": "/loginRegistration/user/"
}
My project structure is...
My Main class is
import org.example.controller.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
#SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
System.out.println("Welcome");
}
}
My Controller is....
import com.fasterxml.jackson.core.JsonProcessingException;
import org.example.entity.Registration;
import org.example.model.UserDto;
import org.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
#RestController
#RequestMapping("/user")
public class User {
#Autowired
UserService userService;
#RequestMapping(value = "/registration", method = RequestMethod.POST)
public String registerUser(#Validated UserDto userdto) throws JsonProcessingException {
userService.registerUser(userdto);
return "success";
}
#RequestMapping(value = "/getUserList", method = RequestMethod.GET)
public List<Registration> getUserList() {
List<Registration> list = userService.getUserList();
return list;
}
#RequestMapping(value = "/")
public String test() {
return "testing";
}
}
Please guide me what changes need to be done to run on postman.
spring application name does not use as context path. You will have to
define below property in your application.properties file to use
'loginRegistration' as your context path.
server.servlet.context-path=/loginRegistration
And now you can use it as /loginRegistration/user/
Let me know if that helps. Thanks
You have to make sure, you providing war name along with your given URL... And see packages should be under you main class... if not u have to add in main class using componentscan
Another alternative to what #Jimmy described is to define #RequestMapping in your User class as -
#RestController
#RequestMapping("/loginRegistration/user/")
public class User {

Returning image from remote server play framework java

I tried to return the image as shown below:
return ok(new File("http://example.com/dpa/client_name/images/client_log.jpg"));
but the method in the controller couldn't fetch the image from the remote server and threw a image not found exception.
How do I retrieve an image from the remote server and return as a response using java play framework?
Simply use WS API
package controllers;
import play.libs.ws.WSClient;
import play.mvc.Controller;
import play.mvc.Result;
import java.util.concurrent.CompletionStage;
import javax.inject.Inject;
public class HomeController extends Controller {
#Inject WSClient ws;
public CompletionStage<Result> index() {
return ws
.url("http://www.maine-coon-cat-nation.com/image-files/orange-maine-coon-cat.jpg")
.get()
.thenApply(file -> ok(file.getBodyAsStream()).as("image/jpeg"));
}
}

Spring MVC in conjunction with angular vanishing view

I try connect SpringMVC, apache tiles, angular and boostrap. After typing: http://localhost:8080/testrest menu appears with Bootstrap, the table in which they appear videos and does not pass even a second it gets a white screen.
Check my video here: https://youtu.be/V9FvL0yUWXU
Rest returns the data, so it's ok.
Code AngularJs:
https://github.com/giecmarcin/[...]/main/webapp/WEB-INF/static/js
View file:
https://github.com/giecmarcin/[...]B-INF/pages/MovieManagment.jsp
RestController:
package com.springapp.mvc.controller;
import com.springapp.mvc.entities.Movie;
import com.springapp.mvc.services.MovieService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* Created by Marcin on 19.04.2016.
*/
#RestController
public class MovieRestController {
#Autowired
MovieService movieService;
#RequestMapping(value = "/movies/all", method = RequestMethod.GET)
public ResponseEntity<List<Movie>> listAllMovies() {
List<Movie> movies = movieService.findAll();
if(movies.isEmpty()){
return new ResponseEntity<List<Movie>>(HttpStatus.NO_CONTENT);//You many decide to return HttpStatus.NOT_FOUND
}
return new ResponseEntity<List<Movie>>(movies, HttpStatus.OK);
}
}
Kontroler zwracajÄ…cy widok
#Controller
public class IndexController {
#RequestMapping("/")
public String index() {
return "index";
}
#RequestMapping("/testrest")
public String getTest(){
return "MovieManagment";
}
}
Thank you for help.

How to add HTTP lifecycle middleware handler to spring?

I have a CustomerController.java:
package com.satisfeet.http;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.satisfeet.core.model.Address;
import com.satisfeet.core.model.Customer;
import com.satisfeet.core.service.CustomerService;
#RestController
#RequestMapping("/customers")
public class CustomerController {
#Autowired
private CustomerService service;
#RequestMapping(method = RequestMethod.GET)
public Iterable<Customer> index() {
return this.service.list();
}
#RequestMapping(method = RequestMethod.POST)
public Customer create(#RequestBody Customer customer) {
this.service.create(customer);
return customer;
}
#RequestMapping(method = RequestMethod.GET, value = "/{id}")
public Customer show(#PathVariable Integer id) {
return this.service.show(id);
}
#RequestMapping(method = RequestMethod.PUT, value = "/{id}")
public void update(#PathVariable Integer id, #RequestBody Customer customer) {
this.service.update(id, customer);
}
#RequestMapping(method = RequestMethod.DELETE, value = "/{id}")
public void destroy(#PathVariable Integer id) {
this.service.delete(id);
}
}
and a ExceptionController.java:
package com.satisfeet.http;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import com.satisfeet.core.exception.NotFoundException;
#ControllerAdvice
public class ExceptionController {
#ExceptionHandler(NotFoundException.class)
public ResponseEntity notFoundError() {
return new ResponseEntity(HttpStatus.NOT_FOUND);
}
}
I would now like to add some sort of HTTP request-response middleware which is executed before the response is written and write the HTTP status code in json:
HTTP/1.1 404 OK
Connection: close
Content-Type: application/json
{"error":"not found"}
I know how to convert a HttpStatus to a String but I do not know where I could do this globally with #ControllerAdvice.
So how do I register a global handler which has access to a response object?
i think what you are looking for is spring Interceptor .
please check this tutorial which is describing how to use interceptors .
and check spring documentation Intercepting requests with a HandlerInterceptor .
finally check this answer here
Hope that helps .
To whom ever this may concern regarding the answer, the class HandlerInterceptorAdapter is now deprecated. You can implement the HandlerInterceptor which comes in the form of an interface instead. You can then implement preHandle, postHandle, or afterCompletion methods. See this question.

Categories