如何在Java中使用API​​ Instagram

时间:2016-05-21 14:53:16

标签: java jsp servlets instagram instagram-api

我使用API​​ jInstagram和Servlets登录Instagram。但是我对提取访问令牌有疑问。

My Class Servlets

 import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jinstagram.Instagram;
import org.jinstagram.auth.model.Token;
import org.jinstagram.auth.model.Verifier;
import org.jinstagram.auth.oauth.InstagramService;

/**
 *
 * @author rafaelcarlos
 */
public class MeuServletInsta extends HttpServlet {

    public static final String SERVICO_INSTA = "instServico";
    public static final String OBJETO_INSTA = "objServico";

    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        String codigo = req.getParameter("code");

        InstagramService servico = (InstagramService) req.getSession().getAttribute(SERVICO_INSTA);

        Verifier verifier = new Verifier(codigo);

        Token token = servico.getAccessToken(null, verifier);

        Instagram insta = new Instagram(token);
        req.getSession().setAttribute(OBJETO_INSTA, insta);

        resp.sendRedirect(req.getContextPath() + "/perfil.jsp");
    }
}

我的login.jsp

<%@page import="br.com.rafael.eventos.insta.MeuServletInsta"%>
<%@page import="org.jinstagram.auth.InstagramAuthService"%>
<%@page import="org.jinstagram.auth.oauth.InstagramService"%>

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
         pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>Login Instagram</title>
        <style type="text/css">

        </style>
    </head>
    <body>

        <%
            InstagramService servico = new InstagramAuthService()
                    .apiKey("a398c75c220d4098aa818ecaf61425d0")
                    .apiSecret("98b27978afe14e969f8b00f1064ee204")
                    .callback("http://localhost:8084/Eventos/token").build();

            String autorizacao = servico.getAuthorizationUrl(null);

            session.setAttribute(MeuServletInsta.SERVICO_INSTA, servico);
        %>

        <div>
            <form action="">
                <a href="<%=autorizacao%>">Clique aqui para logar com o Instagram</a>
            </form>
        </div>

    </body>
</html>

我的perfil.jsp负责显示信息。

<%@page import="org.jinstagram.entity.users.feed.MediaFeedData"%>
<%@page import="java.util.List"%>
<%@page import="org.jinstagram.entity.users.feed.MediaFeed"%>
<%@page import="org.jinstagram.entity.users.basicinfo.UserInfoData"%>
<%@page import="br.com.rafael.eventos.insta.MeuServletInsta"%>
<%@page import="org.jinstagram.Instagram"%>

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Pefil Insta</title>
</head>
<body>

    <%
        Instagram insta = (Instagram) session.getAttribute(MeuServletInsta.OBJETO_INSTA);

            UserInfoData usuario = insta.getCurrentUserInfo().getData();
//          MediaFeed mediaFeed = insta.getRecentMediaFeed("54609412");
    %>

    <h1>Nome Usuário: <%= usuario.getFullName() %></h1>
    <%= usuario.getBio() %><br>
    <%= usuario.getFirst_name() %><br>
    <%= usuario.getId() %><br>
    <img src="<%= usuario.getProfile_picture() %>" /><br>
    <%= usuario.getUsername() %><br>
    <%= usuario.getCounts().getFollows() %><br>
    <%= usuario.getCounts().getFollwed_by() %><br>
    <%= usuario.getCounts().getMedia() %><br>



</body>
</html>

我的设置web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <display-name>Eventos</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>login.jsp</welcome-file>
    </welcome-file-list>

    <servlet>
        <servlet-name>oAuth-Insta</servlet-name>
        <servlet-class>br.com.rafael.eventos.insta.MeuServletInsta</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>oAuth-Insta</servlet-name>
        <url-pattern>/token</url-pattern>   
    </servlet-mapping>
</web-app>

记录错误

21-May-2016 11:41:23.485 SEVERE [http-nio-8084-exec-62] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [oAuth-Insta] in context with path [/Eventos] threw exception
 org.jinstagram.auth.exceptions.OAuthException: Cannot extract an acces token. Response was: {"access_token": "950633271.a398c75.505b55bdeb1547f0bacc522aa7ba2602", "user": {"username": "rafaeltry", "bio": "", "website": "", "profile_picture": "https://igcdn-photos-g-a.akamaihd.net/hphotos-ak-xft1/t51.2885-19/11849334_1455321318109806_1786291315_a.jpg", "full_name": "Rafael Carlos", "id": "950633271"}}
    at org.jinstagram.auth.InstagramApi$1.extract(InstagramApi.java:53)
    at org.jinstagram.auth.oauth.InstagramService.getAccessToken(InstagramService.java:61)
    at br.com.rafael.eventos.insta.MeuServletInsta.doGet(MeuServletInsta.java:32)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:537)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1085)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:658)
    at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1556)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1513)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

1 个答案:

答案 0 :(得分:0)

使用jInstagram v1.1.9,它只是The url couldnt be validated. Callback verification failed with the following errors: HTTP Status Code = 500; HTTP Message = INTERNAL SERVER ERROR 而不是Token accessToken = service.getAccessToken(verifier);