sachin-handiekar / jInstagram

A Java library for the Instagram API.
MIT License
385 stars 167 forks source link

Access token is invalid, although recieved from Instagram during authentication. #176

Closed kernelfreak closed 7 years ago

kernelfreak commented 8 years ago
I am working on a Spring-MVC project in which I am working on Instagram login and pulling other information from Instagram. Currently, the Instagram login part is working quite good, and I get the same access_token back from Instagram whenever I query them. Unfortunately, whenever I call any API's with that token, I get an invalid access_token. What am I doing wrong?

The access_token is of format XXXXX.xxxxx.XXXXXXXX

Login code, controller methods :

@RequestMapping(value = "/connectinsta") public String connectToInsta(@RequestParam("student") boolean type,HttpSession session) { session.setAttribute("type",type); return "redirect:https://api.instagram.com/oauth/authorize/?client_id=" + INSTAGRAM_CLIENT_ID + "&redirect_uri=" + INSTAGRAM_REDIRECT_URI + "&response_type=code"; }

@RequestMapping(value = "/instaredirect")
public String instagramRedirect(HttpServletRequest request, HttpServletResponse response, Model model){
    String code = request.getParameter("code");
    if (!(code == null)) {
        try {
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.MULTIPART_FORM_DATA);

            MultiValueMap<String, String> uriVariables = new LinkedMultiValueMap<>();

            uriVariables.add("redirect_uri", INSTAGRAM_REDIRECT_URI);
            uriVariables.add("client_id", INSTAGRAM_CLIENT_ID);
            uriVariables.add("client_secret", INSTAGRAM_CLIENT_SECRET);
            uriVariables.add("code", code);
            uriVariables.add("grant_type", "authorization_code");

            HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity<MultiValueMap<String, String>>(uriVariables, headers);

            RestTemplate restTemplate = new RestTemplate();

            HttpEntity responseEntity = restTemplate.exchange("https://api.instagram.com/oauth/access_token", HttpMethod.POST, httpEntity, InstagramLogin.class);
            InstagramLogin instagram =  (InstagramLogin) responseEntity.getBody();
            HttpSession session = request.getSession();
            System.out.println("Access token is "+instagram.getAccess_token());
            boolean val = (Boolean)session.getAttribute("type");
            System.out.println("Value is "+val);
            if(val){

                Host host = this.hostService.addHostForInstagram(instagram);
                if(host != null) {

                    if (host.isNewHost()) {
                        model.addAttribute("student",false);

                    } 
                  return "profile-family";
                }else {
                    System.out.println("Host is null");
                }
            }
        }catch (Exception e){
            e.printStackTrace();

        }

    }
    return "redirect:/";
}

Then when I try this code, I get the following error :

@Override public void pullInstagramMedia() { Host host = getCurrentlyAuthenticatedHost(); try { if(host != null){ if((host.isSocialLogin()) && (host.getSocialPlatform().equals("instagram"))) { System.out.println("Host access token is "+host.getAccessToken()); Instagram instagram = new Instagram(host.getAccessToken()); UserInfo userInfo = instagram.getCurrentUserInfo();

                UserInfoData userData = userInfo.getData();
                System.out.println("id : " + userData.getId());
                System.out.println("first_name : " + userData.getFirstName());
                System.out.println("last_name : " + userData.getLastName());
                System.out.println("profile_picture : " + userData.getProfilePicture());
                System.out.println("website : " + userData.getWebsite());

            }
        }
    }catch (Exception e){
        e.printStackTrace();
    }
}

org.jinstagram.exceptions.InstagramBadRequestException: OAuthAccessTokenException: The access_token provided is invalid.
    at org.jinstagram.entity.common.InstagramErrorResponse.throwException(InstagramErrorResponse.java:45)
    at org.jinstagram.Instagram.handleInstagramError(Instagram.java:1187)
    at org.jinstagram.Instagram.createInstagramObject(Instagram.java:1131)
    at org.jinstagram.Instagram.createInstagramObject(Instagram.java:1150)
    at org.jinstagram.Instagram.getCurrentUserInfo(Instagram.java:192)
    at com.journaldev.spring.service.HostServiceImpl.pullInstagramMedia(HostServiceImpl.java:128)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
    at com.sun.proxy.$Proxy69.pullInstagramMedia(Unknown Source)
    at com.journaldev.spring.controller.InstagramController.getInstagramPhotos

I don't understand why there is a problem as the access token is what I received from Instagram and I have verified is that's the only one getting used and it is. Any help on this would be nice. thank you.

sembozdemir commented 8 years ago

I think you should add scope "basic" to read a user's own data. Have you added the scope? For more information: https://www.instagram.com/developer/authorization/

sachin-handiekar commented 7 years ago

Re-open if needed.