我一直在尝试开发一个Play 2.5应用程序来显示instagram图片,方法是将hashtag作为输入并调用instagram rest API并显示结果。我计划在使用Postgres数据库的Heroku上部署应用程序。
为了显示图片,我打算在视图中使用图片代码中的网址字符串<img src=@collection.img/>
但是当我在本地运行应用程序时,图像不会显示,并且localhost将附加到src属性。
src = http://localhost:9000/%22https://scontent.cdninstagram.com/t51.2885-15/s640x640/sh0.08/e35/10296636_1024245191001256_1641267310_n.jpg?ig_cache_key=MTIwODg5NTI5ODg4NDk5NDk1Ng%3D%3D.2%22/
1.我尝试安装CORS插件并允许https://scontent.cdninstagram.com/ * url通过。
2.我尝试在application.conf中更改cors的选项: -
cors {
# Filter paths by a whitelist of path prefixes
#pathPrefixes = ["/some/path", ...]
# The allowed origins. If null, all origins are allowed.
allowedOrigins = [".instagram.com", "localhost:9000",".cdninstagram.com"]
# The allowed HTTP methods. If null, all methods are allowed
allowedHttpMethods = ["GET", "POST"]
}
hosts {
# Allow requests to example.com, its subdomains, and localhost:9000.
allowed = [".instagram.com", "localhost:9000",".cdninstagram.com"]
}
这是我控制Instagram API的控制器:
Collection collection = formFactory.form(Collection.class).bindFromRequest().get();
Collection cocoTasks = Collection.find.where()
.ilike("name",collection.name)
.findUnique();
Node res = printCollection(cocoTasks);
for(JsonNode coll : res) {
ResultCollection c1 = new ResultCollection();
c1.setcName(collection.name);
c1.setURLName(coll.get("link").toString());
c1.setImg(coll.get("images").get("standard_resolution").get("url").toString());
c1.setInstagramUsername(coll.get("user").get("username").toString());
c1.save();
@Transactional
public JsonNode printCollection(Collection c) throws InterruptedException,ExecutionException{
CompletionStage<JsonNode> json = getCollectionHelper(c);
CompletableFuture<JsonNode> res1 =json.toCompletableFuture();
JsonNode jArray = res1.get();
JsonNode res2 = jArray.get("data");
return res2;
}
@Inject WSClient ws;
public CompletionStage<JsonNode> getCollectionHelper(Collection c) {
String feedUrl = "https://api.instagram.com/v1/tags/"+c.hashTag+"/media/recent?access_token=my_access_token";
WSRequest request = ws.url(feedUrl);
CompletionStage<JsonNode> output = request
.setRequestTimeout(-1).setQueryParameter("count","20").get()
.thenApply(response -> response.asJson());
return output;
}
这是我的Collection模型类:
public class Collection extends Model{
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
public String id;
public String name;
public String hashTag;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getHashTag() {
return hashTag;
}
public void setHashTag(String hashTag) {
this.hashTag = hashTag;
}
public static Finder<String, Collection> find = new Finder<String,Collection>(Collection.class);
}
这些设置似乎都不起作用。我可以流式传输图像并将其存储在本地数据库中,但除非绝对必要,否则不会选择该选项。 有什么我想念的吗? 感谢。