在android应用程序中显示用户的youtube数据,如用户喜欢的视频和订阅

iyfamqjs  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(228)

我要实现的是:
我正在尝试开发一个android应用程序,它使用google signin登录用户,然后要求用户授予youtube的scope访问权限,这样我的应用程序就可以访问并获取用户的订阅和youtube数据api提供的其他数据。我已经设置了google开发者的控制台项目,用户正在登录,但是当我请求添加youtube的范围时,屏幕会在登录后出现,并且会继续加载。我还启用了youtube数据api v3,并在开发人员控制台中将作用域添加到oauth中。这是密码。它是在androidstudio中编写的本地java代码。而且,我在网上到处搜索,但我找不到任何解决办法,也许我不明白。

public class MainActivity extends AppCompatActivity{

private static final String TAG = "tagg";

// Bundle key for account object
private static final String KEY_ACCOUNT = "key_account";

// Request codes
private static final int RC_SIGN_IN = 9001;
private static final int RC_RECOVERABLE = 9002;
private static final int RC_REQUEST_PERMISSION_SUCCESS_CONTINUE_FILE_CREATION = 0011;

private Account mAccount;

// Global instance of the HTTP transport
private static final HttpTransport HTTP_TRANSPORT = AndroidHttp.newCompatibleTransport();

// Global instance of the JSON factory
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();

private GoogleSignInClient mGoogleSignInClient;

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

    GoogleSignInOptions gso = new GoogleSignInOptions
            .Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            //.requestScopes(new Scope(YOUTUBE_SCOPE))
            .requestEmail()
            .requestIdToken("992631576722-dkn5pp9du25idplr9ll0vkub8pa1sq95.apps.googleusercontent.com")
            .build();

    mGoogleSignInClient = GoogleSignIn.getClient(this, gso);

    findViewById(R.id.signinbtn).setOnClickListener(v -> {
        signIn();
    });

    findViewById(R.id.logout).setOnClickListener(v -> {
        mGoogleSignInClient.revokeAccess();
    });
}

@Override
public void onStart() {
    super.onStart();

    GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
    if (account==null) Toast.makeText(this,"Not Signed",Toast.LENGTH_SHORT).show();

}
private void signIn() {
    Intent signInIntent = mGoogleSignInClient.getSignInIntent();
    startActivityForResult(signInIntent, RC_SIGN_IN);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
    if (requestCode == RC_SIGN_IN) {
        Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
        if (task==null) Toast.makeText(getApplicationContext(),"Null task",Toast.LENGTH_SHORT)
        .show();
        else {
            if (!GoogleSignIn.hasPermissions(
                    task.getResult(),new Scope(YouTubeScopes.YOUTUBE_FORCE_SSL))) {
                    GoogleSignIn.requestPermissions(
                        MainActivity.this,
                        RC_REQUEST_PERMISSION_SUCCESS_CONTINUE_FILE_CREATION,
                        task.getResult(),
                            new Scope(YouTubeScopes.YOUTUBE_FORCE_SSL));
            } else {
            //task.addOnSuccessListener(googleSignInAccount -> Log.d(TAG, "onSuccess: task 
        successful"))
              //   .addOnFailureListener(e -> Log.d(TAG, "onFailure: "+e.getLocalizedMessage()));
                handleSignInResult(task);
            new Thread(() -> {
                try {
                    Log.d(TAG, "the token: "+GoogleAuthUtil
                            .getToken(getApplicationContext(),
                                    task.getResult().getAccount(),YouTubeScopes.YOUTUBE_READONLY));
                } catch (IOException | GoogleAuthException e) {
                    Log.d(TAG, "token failed: "+e.getLocalizedMessage());
                }
            }).start();
            //}
        }
    }
}

private void handleSignInResult(@NonNull Task<GoogleSignInAccount> completedTask) {
    Log.d(TAG, "handleSignInResult:" + completedTask.isSuccessful());

    try {
        GoogleSignInAccount account = completedTask.getResult(ApiException.class);
        // Store the account from the result
        mAccount = account.getAccount();
        account.requestExtraScopes(new Scope(YouTubeScopes.YOUTUBE_READONLY));
        Log.d(TAG, "id token: "+account.getIdToken());
        // Asynchronously access the Youtube API for the account
        new GetSubscriptionTask().execute(mAccount);

    } catch (ApiException e) {
        Log.d(TAG, "handleSignInResult:error"+e.getStatusCode());

        // Clear the local account
        mAccount = null;
    }
}

/**
 * AsyncTask that uses the credentials from Google Sign In to access Youtube subscription API.
 */
private class GetSubscriptionTask extends AsyncTask<Account, Void, List<Subscription>> {

    @Override
    protected List<Subscription> doInBackground(Account... params) {
        Log.d(TAG, "doInBackground: ");
        try {
            GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(
                    MainActivity.this,
                    Collections.singleton(YouTubeScopes.YOUTUBE_READONLY));
            credential.setSelectedAccount(params[0]);

            YouTube youtube = new YouTube.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
                    .setApplicationName("Get User's Own Channel")
                    .build();

            ChannelListResponse channelListResponse = youtube
                    .channels()
                    .list("id,contentDetails")
                    .setMine(true)
                    .setFields("items(contentDetails/relatedPlaylists/uploads,id)")
                    .execute();

            // get signed user channel id:
            Channel myChannel = channelListResponse.getItems().get(0);
            String channelId = myChannel.getId(); // this is user's channel ID

            Log.d(TAG, "my youtube channel id: " + channelId);

            SubscriptionListResponse connectionsResponse = youtube
                    .subscriptions()
                    .list("snippet")
                    .setChannelId(channelId)
                    .execute();

            return connectionsResponse.getItems();
        } catch (UserRecoverableAuthIOException userRecoverableException) {
            Log.d(TAG, "getSubscription:recoverable exception"+userRecoverableException.getLocalizedMessage());
            startActivityForResult(userRecoverableException.getIntent(), RC_RECOVERABLE);
        } catch (IOException e) {
            Log.d(TAG, "getSubscription:exception"+e.getLocalizedMessage());
        }

        return null;
    }

    @Override
    protected void onPostExecute(List<Subscription> subscriptions) {

        if (subscriptions != null) {
            Log.d(TAG, "subscriptions : size=" + subscriptions.size());
            for (Subscription subscription : subscriptions) {
                Log.v(TAG, "subscription : " + subscription.getId());
            }
        } else {
            Log.d(TAG, "subscriptions: null");
        }
    }
}

布局上有一个登录和注销按钮。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题