lmariscal / twitchirc

Twitch Bot Development made Easier | DEPRECATED
Other
40 stars 15 forks source link

if (user.isSubscribed(channel, Screts.oauth_key) == true {} crash #27

Closed chrisderwahre closed 6 years ago

chrisderwahre commented 6 years ago

Hey everyone, i have an issue if i check for the subscribed == true here the full event:

@Override
        protected void userJoins (User user, Channel channel){
            if (user.isSubscribed(channel, Secrets.key) == true) {
                this.sendMessage("Welcome back Subscriber! " + user, channel);
            } else if (user.isMod(channel) == true) {
                this.sendMessage("Hey vsauce Mod " + user + "!", channel);
            } else if (user.isFollowing(channel) == true) {
                this.sendMessage("Hey vsauce " + user + "! Was geht und willkommen im Stream!", channel);
            }
        }

here the error:

java.io.IOException: Server returned HTTP response code: 401 for URL: https://api.twitch.tv/kraken/channels/channel/subscriptions/user?oauth_token=x
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1876)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
    at com.cavariux.twitchirc.Chat.Channel.isSubscribed(Channel.java:286)
    at com.cavariux.twitchirc.Chat.User.isSubscribed(User.java:112)
    at core.FritzderWahre.userJoins(FritzderWahre.java:210)
    at com.cavariux.twitchirc.Core.TwitchBot.start(TwitchBot.java:313)
    at core.main.main(main.java:14)

am i doing something wrong or is it an issue with the API?

mvarendorff commented 6 years ago

Have you set a client ID?

chrisderwahre commented 6 years ago

You mean like this? public Bot() { this.setUsername(Secrets.username); this.setOauth_Key(Secrets.key); this.setClientID(Secrets.clientID); }

mvarendorff commented 6 years ago

Yeah like that. I will have a look into it ^^

mvarendorff commented 6 years ago

Oh yeah the implementation is experimental and apparently incorrect. I might make a commit tomorrow fixing the issue. Further the OAuth token must have been required with scope channel_subscriptions according to the Docs here

chrisderwahre commented 6 years ago

Ok sound good.

lmariscal commented 6 years ago

Yeah, I'm so sorry for this, when I implemented it I had no one who was a partner to test it on so I made it really similar to the follower and followed the docs that may have changed since.

mvarendorff commented 6 years ago

I created the patch now on the brach "subpatch". I have the same issue like cavariux tho. As I am not subscribed to any channels nor know anyone, I cannot test it. An important note: The code requires your OAuth-token to include the scope channel_check_subscription! Meaning that the OAuth token acquired for the bot does NOT have sufficient permissions and will return 401 - Unauthorized. I am not entirely sure how to obtain such a token unfortunately (guess you will have to create your own application online and let people authorize it to acquire such a token) and if it has to be agreed on by the user or the channel-owner.

chrisderwahre commented 6 years ago

i have currently no time,you can try it with the channel 'fameundriches' (im subscribed to this channel (my twitch name is 'ChrisderWahre' too.))

mvarendorff commented 6 years ago

Alright I found some mistakes in my code and stuff but the main issue is the oAuth token. I found a website that lets you acquire such a token here.

After my understanding you have to do the following: Go to your twitchapps here, create a new application with the redirect url given on the website (https://twitchapps.com/tokengen/) and note the client ID of that (or if your bot doesn't have a redirect url you can use your bot for that as well and set its redirect url to that). Then you ask the person whose channel you want to know the subscription of, to open that website and fill in the following: Client-ID: The noted clientId of your project Required Scope: channel_check_subscription Then ask them to click "Connect with Twitch". This will bring them to a confirmation site that will ask them if they agree that it can read their channels subscriptions. If they confirm that, they will see a website with a small field containing the oAuth token you need to provide if you want to check if someone is subscribed to their channel.

mvarendorff commented 6 years ago

@chrisderwahre If you could verify that the above process is working, that would be much appreciated :)

chrisderwahre commented 6 years ago

Ill have a look at it. Give me a Minute.

chrisderwahre commented 6 years ago

Result:

java.io.IOException: Server returned HTTP response code: 401 for URL: https://api.twitch.tv/kraken/channels/119606589/subscriptions/143994027
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1876)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
    at com.cavariux.twitchirc.TwitchAPIConnections.ChannelQueries.isSubscriber(ChannelQueries.java:30)
    at com.cavariux.twitchirc.Chat.Channel.isSubscribed(Channel.java:289)
    at com.cavariux.twitchirc.Chat.User.isSubscribed(User.java:110)
    at core.FritzderWahre.onMessage(FritzderWahre.java:69)
    at com.cavariux.twitchirc.Core.TwitchBot.start(TwitchBot.java:346)
    at core.main.main(main.java:44)

Code:

if (message.contains("subscheck")) {
                if (user.isSubscribed(channel, Secrets.suboauth) == true) {
                    this.sendMessage("@" + user + " You are a Sub, Cool!", channel);
                } else {
                    this.sendMessage("@" + user + " You are not a Sub, not Cool!", channel);
                }
            }
mvarendorff commented 6 years ago

Would have been too easy anyway ;) I will include this for my March run in this repo and by then hopefully found someone who agreed on letting me use their token.

chrisderwahre commented 6 years ago

Hey @geisterfurz007 , i would let you use my Token, do you have Discord or Twitter? My Discord: ChrisderWahre#9694 my Twitter: ChrisderWahre. :)

mvarendorff commented 6 years ago

I contacted you on Discord :) Thanks for the offer already!

mvarendorff commented 6 years ago

This still requires testing. If someone is close with a streamer who has subscribers, please do the following:

Go to your twitchapps here, create a new application with the redirect url given on the website (https://twitchapps.com/tokengen/) and note the client ID of that (or if your bot doesn't have a redirect url you can use your bot for that as well and set its redirect url to that). Then you ask the person whose channel you want to know the subscription of, to open that website and fill in the following: Client-ID: The noted clientId of your project Required Scope: channel_check_subscription Then ask them to click "Connect with Twitch". This will bring them to a confirmation site that will ask them if they agree that it can read their channels subscriptions. If they confirm that, they will see a website with a small field containing an oAuth token. Ask them to give it to you.

Use the token you got from them in the method isSubscribed or contact me on discord under geisterfurz007#5952.

Squidkingdom commented 6 years ago

I'll test the sub things, I'm recently affiliated. (This was the proposal that I forgot about earlier)

mvarendorff commented 6 years ago

Thanks a bunch to @Squidkingdom for going through this! This method should now be fixed and the Experimental markings are removed from the Javadoc with 902d01 and 18f63f on branch subpatch.