Java CommentThreads:insert给出403禁止

Java CommentThreads:insert给出403禁止,java,android,youtube-api,youtube-data-api,Java,Android,Youtube Api,Youtube Data Api,这个错误使我发疯。据我所知,我已经正确地遵循了说明。我的作用域是YOUTUBE\u FORCE\u SSL。在绝望中,我试图在没有运气的情况下添加所有Google Plus范围。仍然会在设备、emulator和GoogleAPI浏览器中出现相同的错误。我试图评论的视频是公开的。我有一个Google+个人资料,当我试图发表评论时,我会用它登录 这是全部错误: com.google.api.client.googleapis.json.GoogleJsonResponseException: 403

这个错误使我发疯。据我所知,我已经正确地遵循了说明。我的作用域是YOUTUBE\u FORCE\u SSL。在绝望中,我试图在没有运气的情况下添加所有Google Plus范围。仍然会在设备、emulator和GoogleAPI浏览器中出现相同的错误。我试图评论的视频是公开的。我有一个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());
                }
这是我的代码:

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());
                }
试试看{
HashMap参数=新的HashMap();
参数put(“部分”、“片段”);
CommentThread CommentThread=新CommentThread();
CommentThreadSnippet snippet=新CommentThreadSnippet();
注释topLevelComment=新注释();
CommentSnippet CommentSnippet=新的CommentSnippet();
set(“textOriginal”,textComment);
commentSnippet.set(“channelId”,channelId);
commentSnippet.set(“videoId”,ytId);
setSnippet(commentSnippet);
setTopLevelComment(topLevelComment);
commentThread.setSnippet(代码段);
YouTube.CommentThreads.Insert commentThreadsInsertRequest=mService.CommentThreads().Insert(parameters.get(“部分”),commentThread);
CommentThread response=commentThreadsInsertRequest.execute();
Log.i(“COMMENT:,response.toString());
}
从Api资源管理器添加屏幕截图:

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浏览器吗?如果是,怎么做

我已经看到了一个类似问题的答案,但它们并不能解决这个问题

感谢您的帮助

编辑1

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

如果我切换到YouTube品牌帐户,这也会起作用

问题仍然存在,它不适用于所有谷歌账户,即使它们也是谷歌+账户。该错误似乎暗示该请求并非来自Google+帐户。如果谷歌能澄清确切的原因,那就太好了

此外,在请求帐户所有者的许可后,是否可以通过编程使帐户有资格发表评论?怎么做?

编辑2

根据分析,错误的原因如下:

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

如何在应用程序中实现这一点

编辑3

我想答案是可以找到的。没有YouTube频道,你无法发表评论

问题是,除非你有一个私人YouTube频道或使用你的品牌帐户登录,否则你无法发表评论。使用谷歌在说明中给出的登录模式不允许使用品牌帐户登录,因为它们在帐户选择器中不可见

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

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频道帐户


因为你不能在没有YouTube私人频道的情况下发表评论(见上面的编辑),所以这个解决方案看起来是这样的。如果你能找到一个更好的,请提交

1) 抓住错误。发出警告和指示

2) 使用以下URL启动Web视图:

3) 监视Webview并确定URL何时更改为以下内容:。URL表示已创建频道

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

已找到上面的URL,但要捕获的错误代码与该页面上的错误代码不同。你应该抓到一个带有“原因”的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。当用户点击“在警报中创建”时,打开此网络视图

请注意,上面的警报中的启动意图代码:

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

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

我也遇到了同样的问题,然后我手动发布了评论,它开始工作了