【问题标题】:401 unauthorized while using YouTube Data API V3使用 YouTube 数据 API V3 时出现未经授权的 401
【发布时间】:2013-03-26 08:01:36
【问题描述】:

我以为我已经成功实现了 YouTube 数据 API 以从我的应用上传视频,直到我不断收到 com.google.api.client.googleapis.json.GoogleJsonResponseException: 401 Unauthorized,对于实现我尝试关注Tasks android sampleYouTube Java sample 以及来自网络的其他示例。任何人都可以找出导致问题的原因吗?

public class splash{

    AccountManager.get(getApplicationContext()).getAuthTokenByFeatures(
                    "com.google", "oauth2:https://gdata.youtube.com", null,
                    this, null, null, new AccountManagerCallback<Bundle>() {

                        @Override
                        public void run(AccountManagerFuture<Bundle> future) {
                            try {
                                Bundle bundle = future.getResult();
                                String acc_name = bundle
                                        .getString(AccountManager.KEY_ACCOUNT_NAME);
                                String auth_token = bundle
                                        .getString(AccountManager.KEY_AUTHTOKEN);



                                Log.d("", "name: " + acc_name + "; token: "
                                        + auth_token);

                                editor.putString("USER_ACCOUNT_PREF", acc_name);
                                editor.putString("USER_ACCOUNT_AUTH_PREF", auth_token);

                            } catch (Exception e) {
                                Log.e("", e.getClass().getSimpleName() + ": "
                                        + e.getMessage());
                            }
                        }
                    }, null);

来自

public class myuploadclass {

    private static YouTube youtube;
    GoogleAccountCredential credential;
    GoogleCredential CREDENTIAL = new GoogleCredential();
    com.google.api.services.youtube.YouTube service;
    final HttpTransport transport = AndroidHttp.newCompatibleTransport();
    final JsonFactory jsonFactory = new GsonFactory();
    final String CLIENT_ID = "blah.apps.googleusercontent.com";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        String test = mPreferences.getString("USER_ACCOUNT_AUTH_PREF", null);
        AccountManager.get(getApplicationContext()).invalidateAuthToken("com.google", test);
        final SharedPreferences.Editor editor = mPreferences.edit();
        AccountManager.get(getApplicationContext()).getAuthTokenByFeatures(
                "com.google", "oauth2:https://gdata.youtube.com", null,
                this, null, null, new AccountManagerCallback<Bundle>() {
                    @Override
                    public void run(AccountManagerFuture<Bundle> future) {
                        try {
                            Bundle bundle = future.getResult();
                            String acc_name = bundle
                                    .getString(AccountManager.KEY_ACCOUNT_NAME);
                            String auth_token = bundle
                                    .getString(AccountManager.KEY_AUTHTOKEN);               

                            Log.d("", "name: " + acc_name + "; token: "
                                    + auth_token);

                            editor.putString("USER_ACCOUNT_PREF", acc_name);
                            editor.putString("USER_ACCOUNT_AUTH_PREF", auth_token);

                        } catch (Exception e) {
                            Log.e("", e.getClass().getSimpleName() + ": "
                                    + e.getMessage());
                        }
                    }
                }, null);
        editor.commit();        

        String test2 = mPreferences.getString(
                "USER_ACCOUNT_AUTH_PREF", null);

        Log.i(mPreferences.getString(
                "USER_ACCOUNT_PREF", null),mPreferences.getString(
                        "USER_ACCOUNT_AUTH_PREF", null));

        credential = GoogleAccountCredential.usingOAuth2(this,
                YouTubeScopes.YOUTUBE);
        credential.setSelectedAccountName(mPreferences.getString(
                "USER_ACCOUNT_PREF", "chrisdunne92@gmail.com"));    

        CREDENTIAL.setAccessToken(test2);

        youtube = new YouTube.Builder(
                transport, jsonFactory, credential).setApplicationName(
                "application name").setHttpRequestInitializer(CREDENTIAL)
                .setGoogleClientRequestInitializer(new YouTubeRequestInitializer(CLIENT_ID))
                .build();

Logcat 输出

    03-26 07:40:15.890: W/System.err(13965): com.google.api.client.googleapis.json.GoogleJsonResponseException: 401 Unauthorized
{"code": 401,"errors": [{"domain": "global","location": "Authorization","locationType": "header","message": "Invalid Credentials","reason": "authError" }],"message": "Invalid Credentials"}

【问题讨论】:

  • "message": "Invalid Credentials" - 消息看起来非常清晰和明显。
  • 我知道我似乎在浪费时间,但是我已经检查了很多东西,我想知道它们是否是我获取令牌的问题,使其无效,如果范围错误或任何其他任意细节。我一遍又一遍地检查并改变了事情,但我仍然得到相同的回应。我的代码似乎是标准的,有很多例子。
  • @RedChris .. 我无法弄清楚是什么让你的代码工作。你能帮我解决这个问题,因为我面临同样的问题。谢谢
  • 我稍后会查看我的代码并尝试找到明确的答案,但这完全与凭据以及我如何创建它有关

标签: android youtube-api


【解决方案1】:

我的问题已经解决了

credential = GoogleAccountCredential.usingOAuth2(this,
    YouTubeScopes.YOUTUBE_UPLOAD,YouTubeScopes.YOUTUBEPARTNER,YouTubeScopes.YOUTUBE);       
    credential.setSelectedAccountName(mPreferences.getString(PREF_ACCOUNT_NAME, null));
    youtube = new YouTube.Builder(
    transport, jsonFactory, credential).setApplicationName(
    "PoliceWitness").build();

【讨论】:

  • 您能否详细说明您的答案,以便我投赞成票
  • 我按照您的回答执行了相同的步骤,但是这种方法已被弃用并且不适用于我的方法。我的意思是,我得到了相同的结果。
  • 如果是这种情况,我不确定要推荐什么。大约一年前,我停止了这个项目。我唯一的想法是尝试找到一些完整的例子。
猜你喜欢
  • 1970-01-01
  • 2020-12-17
  • 2017-11-28
  • 1970-01-01
  • 1970-01-01
  • 2013-04-02
  • 1970-01-01
  • 2014-10-09
  • 1970-01-01
相关资源
最近更新 更多