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

问题描述 投票:14回答:4

我实现了一个Android应用程序,使用户可以直接从应用程序流式传输到YouTube频道。我创建了一个API密钥和一个OAuth 2.0客户端ID enter image description here

但是我得到了以下异常:com.google.android.gms.auth.GoogleAuthException: UNREGISTERED_ON_API_CONSOLE,当我尝试创建一个事件或者当我尝试获取在youtube频道上手动创建的事件时。

我使用以下代码创建一个youtube对象

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();

然后创建一个事件:

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

我显然做错了什么,但我无法弄清楚是什么。任何帮助表示赞赏。提前致谢

android android-youtube-api
4个回答
14
投票

问题在于,当您调试时,您正在使用在〜/ .android / debug.keystore中创建的密钥库,而不是您认为正在使用的任何签名密钥。

当您生成密钥(例如发布已签名的APK)时,您认为此SHA1是Google API界面所需的密钥。事实并非如此。

如果用你的签名密钥替换〜/ .android文件夹中的那个,它就会损坏,因为它缺少androiddebugkey。仅供参考,自动生成密钥的默认密码是“android”。

有关密钥库所在位置的说明,请参阅“调试证书过期”下的https://developer.android.com/studio/publish/app-signing.html

你要做什么:

1)删除debug.keystore并重新启动IDE。这应生成一个带有键别名“androiddebugkey”的新debug.keystore。

2)如果您的IDE没有生成新的密钥库,请重新运行您的Android应用程序。它应该在〜/ .android /中生成它

3)导航到/ path / to / jre / bin并将此路径添加到系统环境变量中。这将允许您访问keytool

4)导航到调试密钥库的目录并运行以下命令:keytool -list -keystore debug.keystore -alias androiddebugkey

5)您的控制台将提示您输入密钥库密码(它是“android”)。

6)从密钥库中获取SHA1密钥并将THAT KEY放入API接口,你会发现它有效。


6
投票

在我的情况下,UNREGISTERED_ON_API_CONSOLE错误是由AndroidManifest中的包名称拼写错误引起的。很简单但却失去了很多时间与钥匙和SHA挣扎。


2
投票

在此之前使用答案,我使用它作为参考来理解和修复我的,并使我如何修复我的更简单的逐步程序。

在Windows命令提示符下。

导航到您的java bin目录。

C:\ Program Files \ Java \ jdk1.8.0_111 \ bin>

并输入ff。命令

keytool -list -v -keystore "%USERPROFILE%\.android\debug.keystore" -alias androiddebugkey -storepass android -keypass android

然后运行ff。码

keytool -list -keystore  "%USERPROFILE%\.android\debug.keystore" -alias androiddebugkey

在询问密码时,输入“android”(不带双引号)

上面代码生成的SHA1密钥。复制它,并将其粘贴到您的谷歌云控制台here

enter image description here

在谷歌云控制台网页上执行此操作

在左侧的选项卡上,找到并单击“API和服务”

然后在新页面上,再次在左侧选项卡上,找到并单击“凭据”

现在复制粘贴从Windows命令提示符复制的密钥,在“签名证书指纹”下面的文本框中

确保您的应用和Google云端控制台上的应用ID相互匹配。

enter image description here


0
投票

我有这个问题,经过大量的搜索,在build.gadle我的applicationId与我在Google控制台上推出的软件包名称不同

defaultConfig {
    applicationId "br.com.glicado.glicado" <-- WAS WRONG, IN MY CASE THE RIGHT IS "br.com.glicado"
    minSdkVersion 15
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
© www.soinside.com 2019 - 2024. All rights reserved.