doesn't work with Twitch
n0xena opened this issue · comments
Code example
import com.google.api.client.auth.oauth2.AuthorizationCodeFlow;
import com.google.api.client.auth.oauth2.BearerToken;
import com.google.api.client.auth.oauth2.ClientParametersAuthentication;
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.util.store.DataStoreFactory;
import com.google.api.client.util.store.FileDataStoreFactory;
import com.google.api.services.youtube.YouTubeScopes;
import java.awt.Desktop;
import java.io.File;
import java.net.URI;
import java.util.Arrays;
import java.util.Collection;
import java.util.Properties;
public final class Auth
{
public final static void main(final String... args) throws Exception
{
Properties properties=new Properties();
properties.load(Auth.class.getResourceAsStream("/oauth/twitch.properties"));
HttpTransport httpTransport=new NetHttpTransport();
JsonFactory jsonFactory=new JacksonFactory();
GenericUrl tokenUrl=new GenericUrl(properties.getProperty("tokenUrl"));
ClientParametersAuthentication clientParametersAuthentication=new ClientParametersAuthentication(properties.getProperty("clientId"), properties.getProperty("clientSecret"));
DataStoreFactory dataStoreFactory=new FileDataStoreFactory(new File("D:/data/java/oauth"));
Collection<String> scopes=Arrays.asList("analytics:read:extensions",
"analytics:read:games",
"bits:read",
"channel:edit:commercial",
"channel:read:hype_train",
"channel:read:subscriptions",
"clips:edit",
"user:edit",
"user:edit:broadcast",
"user:edit:follows",
"user:read:broadcast",
"user:read:email",
"channel:read:stream_key",
"channel_check_subscription",
"channel_commercial",
"channel_editor",
"channel_feed_edit",
"channel_feed_read",
"channel_read",
"channel_stream",
"channel_subscriptions",
"collections_edit",
"communities_edit",
"communities_moderate",
"openid",
"user_blocks_edit",
"user_blocks_read",
"user_follows_edit",
"user_read",
"user_subscriptions",
"viewing_activity_read",
"channel:moderate",
"chat:edit",
"chat:read",
"whispers:read",
"whispers:edit");
AuthorizationCodeFlow authorizationCodeFlow=(new AuthorizationCodeFlow.Builder(BearerToken.authorizationHeaderAccessMethod(), httpTransport, jsonFactory, tokenUrl, clientParametersAuthentication, properties.getProperty("clientId"), properties.getProperty("authUrl"))).setDataStoreFactory(dataStoreFactory).setScopes(scopes).build();
Desktop.getDesktop().browse(new URI(authorizationCodeFlow.newAuthorizationUrl().setRedirectUri("callback uri").build()));
Credential credential=authorizationCodeFlow.createAndStoreCredential(authorizationCodeFlow.newTokenRequest(System.console().readLine("authCode: ")).setRedirectUri("callback uri").execute(), "twitch");
}
}
clientId=client-id
clientSecret=client-secret
authUrl=https://id.twitch.tv/oauth2/authorize
tokenUrl=https://id.twitch.tv/oauth2/token
Stack trace
Exception in thread "main" java.lang.IllegalArgumentException: key scope
at com.google.api.client.json.JsonParser.parseValue(JsonParser.java:900)
at com.google.api.client.json.JsonParser.parse(JsonParser.java:360)
at com.google.api.client.json.JsonParser.parse(JsonParser.java:335)
at com.google.api.client.json.JsonObjectParser.parseAndClose(JsonObjectParser.java:79)
at com.google.api.client.json.JsonObjectParser.parseAndClose(JsonObjectParser.java:73)
at com.google.api.client.http.HttpResponse.parseAs(HttpResponse.java:449)
at com.google.api.client.auth.oauth2.TokenRequest.execute(TokenRequest.java:346)
at de.cryptearth.twitch.Auth.main(Auth.java:69)
Caused by: java.lang.IllegalArgumentException: key scope, field private java.lang.String com.google.api.client.auth.oauth2.TokenResponse.scope
at com.google.api.client.json.JsonParser.parseValue(JsonParser.java:900)
at com.google.api.client.json.JsonParser.parse(JsonParser.java:451)
at com.google.api.client.json.JsonParser.parseValue(JsonParser.java:787)
... 7 more
Caused by: java.lang.IllegalArgumentException: expected collection or array type but got class java.lang.String
at com.google.common.base.Preconditions.checkArgument(Preconditions.java:164)
at com.google.api.client.util.Preconditions.checkArgument(Preconditions.java:67)
at com.google.api.client.json.JsonParser.parseValue(JsonParser.java:724)
... 9 more
External references such as API reference guides
https://dev.twitch.tv/docs/authentication/getting-tokens-oauth#oauth-authorization-code-flow
possible issue: twitch returns scope as array with one string contain scopes split by spaces - hence maybe bad json reply from twitch
as noted on twitchdev/issue#164
according to rfc https://tools.ietf.org/html/rfc6749#section-3.3 the issue is caused by twitch not conforming to rfc as the scope parameter in the token response has to consist only of one string contain the scopes as a space separated list
twitch is violating this rfc by wrapping this string within an additional array - hence closing this issue as it's not a fault of google
sorry for wasting anyones time here