Getting access_token using Java

Hi,
i have the following code(or to be more specific the VAADIN-Framework). I am able to login to twitch via oauth2. I get the code. But when i try to get the accesstoken i receive a “{“error”:“Not Found”,“status”:404,“message”:null}’”

Here the code where i try to get the accesstoken:

package brumm;

import brumm.api.TwitchApi;
import com.github.scribejava.core.builder.ServiceBuilder;
import com.github.scribejava.core.model.*;
import com.github.scribejava.core.oauth.OAuth20Service;
import com.vaadin.annotations.Theme;
import com.vaadin.annotations.Widgetset;
import com.vaadin.server.*;
import com.vaadin.ui.*;
import com.vaadin.ui.themes.ValoTheme;

import java.io.IOException;


public class Thoser666Twitch extends UI implements RequestHandler
{
TextField user = new TextField("User");
PasswordField pwd = new PasswordField("Password");
Button login = new Button("Login");
Link oAuth2;
OAuth20Service service;



@Override
public void init(VaadinRequest request)
{
    // OAuth2-Login

    service = new ServiceBuilder()
            .apiKey("veerkm9f52ru1l7omqk0r2no1kucw3")
            .apiSecret("tiqm12ob0669r03nj5m8a7ayl7oxb2y")
            .callback("http://localhost:8080")
            .scope("chat_login channel_feed_read")
            .state("mullemaus")
            .build(TwitchApi.instance());

    //OAuth2-Login
    String url = service.getAuthorizationUrl();
    oAuth2 = new Link("Login with Twitch", new ExternalResource(url));
    VaadinSession.getCurrent().addRequestHandler(this);

    VerticalLayout layout = new VerticalLayout();
    layout.setSizeFull();
    setContent(layout);
    final MyLoginForm loginForm = new MyLoginForm();
    Component componente = loginForm.createContent(user, pwd, login, oAuth2);
    layout.addComponent(componente);
    layout.setStyleName(ValoTheme.LAYOUT_COMPONENT_GROUP);

    // Loginbutton mit Leben füllen
    login.addClickListener( e -> {
        loginForm.login(user.getValue(), pwd.getValue());
    });


    // Accesstoken holen

System.out.println("Test");
}

@Override
public boolean handleRequest(VaadinSession session, VaadinRequest request, VaadinResponse response)     throws     IOException
{
    if (request.getParameter("code") != null) {
        String code = request.getParameter("code");
        TwitchApi.instance().setCode(code);
        // AccessToken holen
service.getConfig().getApiSecret(), service.getConfig().getCallback(), code, service.getConfig().getState());
        Token t = service.getAccessToken(code);

    }

    return false;
}
}

code for URL’s

package brumm.api;

import com.github.scribejava.core.builder.api.DefaultApi20;
import com.github.scribejava.core.model.OAuthConfig;
import com.github.scribejava.core.model.Verb;

public class TwitchApi extends DefaultApi20
{
OAuthConfig config;

public String getCode()
{
    return code;
}

public void setCode(String code)
{
    this.code = code;
}

private String code;
private static final String AUTHORIZATION_URL
        = "https://api.twitch.tv/kraken/oauth2/authorize" +
        "?response_type=code" +
        "&client_id=%s" +
        "&redirect_uri=%s" +
        "&scope=%s" +
        "&state=%s";

private static final String ACCESSTOKEN_URL
        = "https://api.twitch.tv/kraken/oauth2/token" +
        "&client_id=%s" +
        "&client_secret=%s" +
        "&grant_type=authorization_code" +
        "&redirect_uri=%s" +
        "&code=%s" +
        "&state=%s";

protected TwitchApi()
{

}

private static class InstanceHolder
{
    private static final TwitchApi INSTANCE = new TwitchApi();
}

public static TwitchApi instance()
{
    return InstanceHolder.INSTANCE;
}

@Override
public Verb getAccessTokenVerb() {
    return Verb.POST;
}

@Override
public String getAccessTokenEndpoint()
{
    String tmp = String.format(ACCESSTOKEN_URL, config.getApiKey(), config.getApiSecret(), config.getCallback(), code, config.getState());
    return tmp;
}

@Override
public String getAuthorizationUrl(OAuthConfig oAuthConfig)
{
    config = oAuthConfig;
    return String.format(AUTHORIZATION_URL, oAuthConfig.getApiKey(), oAuthConfig.getCallback(), oAuthConfig.getScope(), oAuthConfig.getState());
}

}

What is wrong?

Hi,

This might be an error in the getAuthorizationUrl, have you checked that all the parameters get filled out correctly and that nothing is null?

EDIT: If you can’t get this code to work I also found this for you to have a look at: [BEGINNER] Authentication for Java App

In addition to what @Dev said, make sure it is POSTing to the URL. Most often, this is an HTTP verb mismatch or simply a URL not being formed properly. The ACCESSTOKEN_URL string looks fine in your code, but I’m not sure what the framework is putting into those values.

1 Like

Thanks Dev. The link showed me the solution. I must use 127.0.0.1: NOT localhost.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.