Search code examples
androidandroid-youtube-api

com.google.android.gms.auth.GoogleAuthException: UNREGISTERED_ON_API_CONSOLE


Im implementing an android app that enables users to stream to a youtube channel straight from the app. I have created an API key and a OAuth 2.0 client ID enter image description here

But I get a the following exeption: com.google.android.gms.auth.GoogleAuthException: UNREGISTERED_ON_API_CONSOLE either when I try to ceate an event or when i try to fetch the one a created manually on the youtube channel.

I use the following code for create a youtube object

String accountName = mContext.getString(R.string.google_account_name);
        String apiKey = mContext.getString(R.string.google_api_key);
        String clientID = mContext.getString(R.string.google_api_client_id);
        String clientName = mContext.getString(R.string.google_api_client_name);

        GoogleAccountCredential credential =
                GoogleAccountCredential.usingOAuth2(mContext,
                        Arrays.asList(YouTubeScopes.YOUTUBE));
        credential.setSelectedAccountName(accountName);

//        String SCOPE = "audience:server:client_id:" + clientID + ":api_scope:" + YouTubeScopes.YOUTUBE;
//        GoogleAccountCredential credential = GoogleAccountCredential.usingAudience(mContext, SCOPE);
//        credential.setSelectedAccountName(accountName);


        youtube = new YouTube.Builder(transport, jsonFactory, credential)
                .setApplicationName(clientName)
                .setYouTubeRequestInitializer(new YouTubeRequestInitializer(apiKey))
                /*.setGoogleClientRequestInitializer(new YouTubeRequestInitializer(apiKey))*/
                .build();

Then to create an event:

LiveBroadcastSnippet broadcastSnippet = new LiveBroadcastSnippet();
broadcastSnippet.setTitle(name);
broadcastSnippet.setScheduledStartTime(new DateTime(futureDate));

LiveBroadcastContentDetails contentDetails = new LiveBroadcastContentDetails();
MonitorStreamInfo monitorStream = new MonitorStreamInfo();
monitorStream.setEnableMonitorStream(false);
contentDetails.setMonitorStream(monitorStream);

// Create LiveBroadcastStatus with privacy status.
LiveBroadcastStatus status = new LiveBroadcastStatus();
status.setPrivacyStatus("unlisted");

LiveBroadcast broadcast = new LiveBroadcast();
broadcast.setKind("youtube#liveBroadcast");
broadcast.setSnippet(broadcastSnippet);
broadcast.setStatus(status);
broadcast.setContentDetails(contentDetails);

// Create the insert request
YouTube.LiveBroadcasts.Insert liveBroadcastInsert = youtube
        .liveBroadcasts().insert("snippet,status,contentDetails",
                            broadcast);
    
// Request is executed and inserted broadcast is returned
LiveBroadcast returnedBroadcast = liveBroadcastInsert.execute(); //<= This line generates the exception

I obviously did something wrong, but I can't figure out what. Any help is appreciated. Thanks in advance


Solution

  • The issue is that, when you debug, you're using a keystore created in ~/.android/debug.keystore, and not whatever signing key you think you're using.

    When you generate a key, such as to release a signed APK, you think that this SHA1 is the one required by the Google API interface. It isn't.

    If you replace the one in the ~/.android folder with your signing key, it's corrupt because it's missing the androiddebugkey. FYI, the default password for the auto-generated key is "android".

    For directions as to where your keystore is located, see https://developer.android.com/studio/publish/app-signing.html under "Expiry of the debug certificate".

    What you have to do:

    1) Delete your debug.keystore and restart your IDE. This should generate a new debug.keystore with key alias "androiddebugkey".

    2) If your IDE does not generate the new keystore, re-run your android application. It should generate it this time in ~/.android/

    3) Navigate to /path/to/jre/bin and add this path to your system environment variables. This will allow you to access keytool.

    4) Navigate to the directory of your debug keystore and run this command: keytool -list -keystore debug.keystore -alias androiddebugkey

    5) Your console will prompt you to enter the keystore password (it is "android").

    6) Get the SHA1 key from the keystore and put THAT KEY into your API interface, and you'll find it works.