I actually got the oauth token and should have tried getting user info with it. But was doing the wrong thing… Sorry;;
Currently I’m using Authentication. I’m getting the access_token however token_type and scope keep comes back as null. Am I keeping something out?
I’m trying to get the user’s login address information. It success in login but the scope in oauthToken returns null. How can I fix this or do I have to do more steps?
Here’s my code in Java.
import com.github.scribejava.core.builder.api.DefaultApi20;
public class TwitchLoginApi extends DefaultApi20{
protected TwitchLoginApi(){
}
private static class InstanceHolder{
private static final TwitchLoginApi INSTANCE = new TwitchLoginApi();
}
public static TwitchLoginApi instance(){
return InstanceHolder.INSTANCE;
}
@Override
public String getAccessTokenEndpoint() {
// TODO Auto-generated method stub
return "https://api.twitch.tv/kraken/oauth2/token?grant_type=authorization_code";
}
@Override
protected String getAuthorizationBaseUrl() {
// TODO Auto-generated method stub
return "https://api.twitch.tv/kraken/oauth2/authorize";
}
}
Controller class
package controller;
import java.io.IOException;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.social.connect.Connection;
import org.springframework.social.google.api.Google;
import org.springframework.social.google.api.impl.GoogleTemplate;
import org.springframework.social.google.api.plus.Person;
import org.springframework.social.google.api.plus.PlusOperations;
import org.springframework.social.google.connect.GoogleConnectionFactory;
import org.springframework.social.oauth2.AccessGrant;
import org.springframework.social.oauth2.GrantType;
import org.springframework.social.oauth2.OAuth2Operations;
import org.springframework.social.oauth2.OAuth2Parameters;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.github.scribejava.core.builder.api.DefaultApi20;
import com.github.scribejava.core.model.OAuth2AccessToken;
import com.github.scribejava.core.model.OAuthConfig;
import service.NaverLoginService;
import service.TwitchLoginService;
@Controller
public class MemberController{
private OAuth2AccessToken oauthToken;
private TwitchLoginService twitchLoginservice;
@Autowired
private void setTwitchLoginService(TwitchLoginService twitchLoginservice) {
this.twitchLoginservice = twitchLoginservice;
}
@RequestMapping(value = "loginForm.do", method = { RequestMethod.GET, RequestMethod.POST })
public String naverlogin(Model model, HttpSession session) {
String twitchAuthUrl = twitchLoginservice.getAuthorizationUrl(session);
model.addAttribute("twitchurl", twitchAuthUrl);
return "member/login";
}
@RequestMapping(value = "twitchCallback.do", method = { RequestMethod.GET, RequestMethod.POST })
public String twitchCallback(Model model, @RequestParam String code, @RequestParam String state, HttpSession session)
throws IOException {
System.out.println("여기는 twitchCallback");
oauthToken = twitchLoginservice.getAccessToken(session, code, state);
System.out.println(oauthToken);
return "member/twitchSuccess";
}
}
Service class
package service;
import java.io.IOException;
import java.util.UUID;
import javax.servlet.http.HttpSession;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import com.github.scribejava.core.builder.ServiceBuilder;
import com.github.scribejava.core.model.OAuth2AccessToken;
import com.github.scribejava.core.model.OAuthRequest;
import com.github.scribejava.core.model.Response;
import com.github.scribejava.core.model.Verb;
import com.github.scribejava.core.oauth.OAuth20Service;
import model.NaverLoginApi;
import model.TwitchLoginApi;
public class TwitchLoginService {
private final static String CLIENT_ID = "ghty8lutmj1064oembb9g8195fr9eg";
private final static String CLIENT_SECRET = "my secret";
private final static String REDIRECT_URI = "http://localhost:8080/InsightGame/twitchCallback.do";
private final static String SCOPE = "user_read";
private String generateRandomString() {
return UUID.randomUUID().toString();
}
private void setSession(HttpSession session,String state){
session.setAttribute("state", state);
}
private String getSession(HttpSession session){
return (String) session.getAttribute("state");
}
public String getAuthorizationUrl(HttpSession session) {
String state = generateRandomString();
setSession(session,state);
OAuth20Service oauthService = new ServiceBuilder()
.apiKey(CLIENT_ID)
.apiSecret(CLIENT_SECRET)
.callback(REDIRECT_URI)
.scope("user_read")
.state(state)
.build(TwitchLoginApi.instance());
//System.out.println(oauthService.getAuthorizationUrl());
return oauthService.getAuthorizationUrl();
}
public OAuth2AccessToken getAccessToken(HttpSession session, String code, String state) throws IOException{
String sessionState = getSession(session);
if(StringUtils.equals(sessionState, state)){
OAuth20Service oauthService = new ServiceBuilder()
.apiKey(CLIENT_ID)
.apiSecret(CLIENT_SECRET)
.callback(REDIRECT_URI)
.scope("user_read")
.state(state)
.build(TwitchLoginApi.instance());
OAuth2AccessToken accessToken = oauthService.getAccessToken(code);
System.out.println(accessToken);
return accessToken;
}
return null;
}
}
The result I get back is
OAuth2AccessToken{access_token=06z2rpaznutjxx0zauquxgm6wbt5hf, token_type=null, expires_in=14015, refresh_token=m3srhojf69v41nv1dtsrvtqy0tqtg1nn7ljta9lgu6ibh7emy9, scope=null}