CommentThreads:insert给出403 Forbidden

时间:2018-06-13 03:12:52

标签: java android youtube-api youtube-data-api

我对此错误感到疯狂。据我所知,我已正确遵循说明。我的范围是YOUTUBE_FORCE_SSL。无奈之下,我试图毫无运气地添加所有Google Plus Scopes。在设备,模拟器和Google Api Explorer中仍然会出现相同的错误。我试着评论的视频是公开的。我有一个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 Explorer添加屏幕截图:

enter image description here

您可以使用CommentThreads: insert使用API​​资源管理器吗?如果是这样,怎么样?

我已经看到了类似问题here的答案,但他们没有解决这个问题。

感谢任何帮助。

修改1

经过进一步测试。我拥有的旧帐户一切正常。我试图看看哪些设置可能不同,到目前为止没有运气。

如果我切换到YouTube品牌帐户,这也有效。

问题仍然存在,它不适用于所有Google帐户,即使它们也是Google+帐户也不适用。该错误似乎意味着该请求不是来自Google+帐户。如果谷歌能澄清确切的原因,那将会很棒。

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

修改2

根据this page,错误的原因是:

  

必须合并用于授权API请求的YouTube帐户   使用用户的Google帐户插入评论或评论帖子。

如何在应用内完成?

编辑3

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

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

结果是您可以使用拥有YouTube品牌帐户的帐户登录,但您无法使用该帐户发表评论,因为您无法选择品牌帐户,因此无法解决除非您要求用户也创建私人频道。错误信息应该是这样的:

  

来电者YouTube帐户不是YouTube频道帐户。

1 个答案:

答案 0 :(得分:1)

由于您无法在没有私人YouTube频道的情况下发表评论(请参阅上面的编辑),解决方案将如下所示。如果您能找到更好的产品,请提交!

1)抓住错误。提供警告说明。

2)使用以下网址启动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请求。

找到上面的网址here,但要捕获的错误代码与该网页上的错误代码不同。您应该使用“reason”:“ineligibleAccount”来捕获403。

更新2018年6月29日

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

<强> 1。在用户发布没有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。当用户点击警报中的CREATE时,打开此WebView

请注意此代码在上面的警告中启动Intent:

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

WebView的XML:

<?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。用户在您的活动中完成“创建频道”步骤时抓住

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

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