CommentThreads:插入给出 403 Forbidden

CommentThreads: insert gives 403 Forbidden

这个错误让我抓狂。据我所知,我已正确按照说明进行操作。我的范围是 YOUTUBE_FORCE_SSL。无奈之下,我尝试添加所有 Google Plus 范围,但没有成功。在设备、模拟器和 Google Api Explorer 中仍然出现相同的错误。我尝试评论的视频是 public。我有一个 Google+ 个人资料,当我尝试发表评论时使用它登录。

这是完整的错误:

com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
    {
      "code" : 403,
      "errors" : [ {
        "domain" : "youtube.commentThread",
        "location" : "Authorization",
        "locationType" : "header",
        "message" : "The callers YouTube account is not connected to Google+.",
        "reason" : "ineligibleAccount"
      } ],
      "message" : "The callers YouTube account is not connected to Google+."
    }

这是我的代码:

try {
                    HashMap<String, String> parameters = new HashMap<>();
                    parameters.put("part", "snippet");


                    CommentThread commentThread = new CommentThread();
                    CommentThreadSnippet snippet = new CommentThreadSnippet();
                    Comment topLevelComment = new Comment();
                    CommentSnippet commentSnippet = new CommentSnippet();
                    commentSnippet.set("textOriginal", textComment);
                    commentSnippet.set("channelId", channelId);
                    commentSnippet.set("videoId", ytId);

                    topLevelComment.setSnippet(commentSnippet);
                    snippet.setTopLevelComment(topLevelComment);
                    commentThread.setSnippet(snippet);

                    YouTube.CommentThreads.Insert commentThreadsInsertRequest = mService.commentThreads().insert(parameters.get("part"), commentThread);

                    CommentThread response = commentThreadsInsertRequest.execute();
                    Log.i("COMMENT:", response.toString());
                }

正在添加来自 Api 资源管理器的屏幕截图:

你能让 CommentThreads: insert 与 API Explorer 一起工作吗?如果是,怎么做?

我看过类似问题的答案,但他们没有解决这个问题。

感谢任何帮助。

编辑 1

经过进一步测试。我拥有的旧帐户一切正常。到目前为止,我已经尝试查看哪些设置可能会有所不同。

如果我切换到 YouTube 品牌帐户,这也适用。

问题仍然存在,它不适用于所有 Google 个帐户,即使它们也是 Google+ 个帐户。该错误似乎暗示请求不是来自 Google+ 帐户。如果 Google 能说明确切原因就好了。

此外,是否可以在征得帐户所有者的许可后,以编程方式使帐户有资格发表评论?怎么样?

编辑 2

根据this page报错的原因是这样的:

The YouTube account used to authorize the API request must be merged with the user's Google account to insert a comment or comment thread.

如何在应用程序中完成此操作?

编辑 3

我想可以找到答案here。没有 YouTube 频道就无法发表评论。

问题是,除非您拥有私人 YouTube 频道或使用您的品牌帐户登录,否则您无法发表评论。使用说明中 Google 提供的模型登录不允许使用品牌帐户登录,它们在帐户选择器中不可见。

结果是您可以使用拥有 YouTube 品牌帐户的帐户登录,但您将无法使用该帐户发表评论,并且由于您无法选择品牌帐户,因此没有办法解决这个问题,除非你要求用户也创建一个私人频道。错误消息应该是这样的:

The callers YouTube account is not a YouTube Channel Account.

由于在没有私人 YouTube 频道的情况下无法 post 发表评论(请参阅上面的修改),因此解决方案看起来像这样。如果您能找到更好的,请提交!

1) 捕获错误。发出带有说明的警报。

2) 使用此 URL 启动 Webview:https://m.youtube.com/create_channel?chromeless=1&next=/channel_creation_done

3) 监视 Webview 并确定 URL 何时更改为以下内容:https://m.youtube.com/channel_creation_done。 URL表示频道已经创建。

4) 关闭 Webview 并重新发送授权的 API 请求。

上面的 URL 已找到 here 但要捕获的错误代码与该页面上的错误代码不同。你应该用 "reason" 捕获 403 : "ineligibleAccount".

2018 年 6 月 29 日更新

我今天回到了这个问题并让它以可接受的方式工作。请参阅下面我的实现:

1.用户 post 在没有 YouTube 频道的情况下发表评论后捕获错误 403

if(mLastError.getMessage().contains("403") && mLastError.getMessage().contains("ineligibleAccount")) {
                        // Show Alert with instruction
                        showAlertCreate("Please Create a YouTube Channel!", "You need a personal YouTube Channel linked to your Google Account before you can comment. Don't worry, it's easy to create one!\n\n1) Tap on CREATE below and wait for page to load.\n\n2) Login if needed.\n\n3) Tap CREATE CHANNEL and wait until comment is posted.");
}

警报代码:

public void showAlertCreate(String title, String description) {
    AlertDialog.Builder builder;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        builder = new AlertDialog.Builder(this, android.R.style.Theme_Material_Dialog_Alert);
    } else {
        builder = new AlertDialog.Builder(this);
    }
    builder.setTitle(title)
            .setMessage(description)
            .setPositiveButton(R.string.yes_create, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // Start Youtube WebView to create Channel
                    Intent intent = new Intent(mContext, WebViewActivity.class);
                    startActivityForResult(intent, 777);
                }
            })
            .setIcon(android.R.drawable.ic_dialog_alert)
            .show();
}

2。当用户在 Alert 中点击 CREATE 时,打开这个 WebView

注意此代码以在上面的警报中启动 Intent:

// Start Youtube WebView to create Channel
                    Intent intent = new Intent(mContext, WebViewActivity.class);
                    startActivityForResult(intent, 777);

XML 对于 WebView:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".WebViewActivity">

    <WebView
        android:id="@+id/create_channel"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</android.support.constraint.ConstraintLayout>

WebView 代码:

public class WebViewActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view);

        WebView createChannel = findViewById(R.id.create_channel);

        createChannel.setWebViewClient(new WebViewClient() {
            public void onPageFinished(WebView view, String url) {
                if (url!=null && url.contains("https://m.youtube.com/channel_creation_done")) {
                    view.setVisibility(View.INVISIBLE);
                    //Log.i("URLWEB", url);
                    Intent intent = new Intent();
                    intent.putExtra("created", "yes");
                    setResult(RESULT_OK, intent);
                    finish();
                }
            }
        });

        createChannel.loadUrl("https://m.youtube.com/create_channel?chromeless=1&next=/channel_creation_done");

    }
}

3。当用户完成 activity

中的创建频道步骤时捕捉

在 onActivityResult() 中包含如下内容:

if (requestCode == 777) {
    if(resultCode == RESULT_OK) {
        // Receive intent from WebView, if new Channel, repost comment/reply
        String created = data.getStringExtra("created");
        if(created.equals("yes")) {
            // Posting the comment again
            getResultsFromApi();
        }
    }
}

不是最干净的解决方案,但它有效。

如果您创建了一个新帐户并且没有对任何 YouTube 视频发表评论,则会抛出错误:“来电者 YouTube 帐户未连接到 Google+。”。 =10=]

解决方案:至少使用新帐户在任何 YouTube 视频中手动发表评论,然后尝试 API。运行流畅。