我想使用Youtube API獲取用戶的訂閱列表。它需要oauth。訪問Youtube API
,我讀了谷歌執行登錄會更容易訪問此API
我跟谷歌的文檔,現在我在工作
I have these files now拿到了簽字儀式。
我的問題:
1)我需要哪個樣品使用,IdTokenActivity.java
或RestApiActivity.java
2)我如何使用示例代碼使用YouTube API?它並沒有說和文件混淆
我想使用Youtube API獲取用戶的訂閱列表。它需要oauth。訪問Youtube API
,我讀了谷歌執行登錄會更容易訪問此API
我跟谷歌的文檔,現在我在工作
I have these files now拿到了簽字儀式。
我的問題:
1)我需要哪個樣品使用,IdTokenActivity.java
或RestApiActivity.java
2)我如何使用示例代碼使用YouTube API?它並沒有說和文件混淆
IdTokenActivity.java
或RestApiActivity.java
?IdTokenActivity.java
旨在檢索id_token
。 id_token
是一個JWT令牌,旨在發送到後端以將用戶認證爲真實(可信)的Google用戶。您可以找到有關後端流程的更多信息here。
RestApiActivity.java
用於消費Google API,這正是您正在嘗試執行的操作。
步驟如下:
轉到Google Signin setup for Android,下載google-services.json
並把它放在你的app
文件夾
在google developer console使YouTube數據API
添加以下到應用程序build.gradle
:
compile 'com.google.android.gms:play-services-auth:10.0.1'
compile 'com.google.api-client:google-api-client-android:1.22.0' exclude module: 'httpclient'
compile 'com.google.apis:google-api-services-youtube:v3-rev182-1.22.0'
與apply plugin: 'com.google.gms.google-services'
到文件的底部
更新的以下到您的頂層build.gradle
:
dependencies {
classpath 'com.google.gms:google-services:3.0.0'
}
附上RestApiActivity.java
在您的項目中更新以下內容:
// Scope for reading user's contacts
private static final String YOUTUBE_SCOPE = "https://www.googleapis.com/auth/youtube";
...
// Configure sign-in to request the user's ID, email address, basic profile,
// and readonly access to contacts.
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestScopes(new Scope(YOUTUBE_SCOPE))
.requestEmail()
.build();
,並在客戶端進行身份驗證(在handleSignInResult
),要求訂閱列表如下:
/**
* 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 void onPreExecute() {
showProgressDialog();
}
@Override
protected List<Subscription> doInBackground(Account... params) {
try {
GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(
RestApiActivity.this,
Collections.singleton(YOUTUBE_SCOPE));
credential.setSelectedAccount(params[0]);
YouTube youtube = new YouTube.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
.setApplicationName("Google Sign In Quickstart")
.build();
SubscriptionListResponse connectionsResponse = youtube
.subscriptions()
.list("snippet")
.setChannelId("UCfyuWgCPu5WneQwuLBWd7Pg")
.execute();
return connectionsResponse.getItems();
} catch (UserRecoverableAuthIOException userRecoverableException) {
Log.w(TAG, "getSubscription:recoverable exception", userRecoverableException);
startActivityForResult(userRecoverableException.getIntent(), RC_RECOVERABLE);
} catch (IOException e) {
Log.w(TAG, "getSubscription:exception", e);
}
return null;
}
@Override
protected void onPostExecute(List<Subscription> subscriptions) {
hideProgressDialog();
if (subscriptions != null) {
Log.d(TAG, "subscriptions : size=" + subscriptions.size());
// Get names of all connections
for (int i = 0; i < subscriptions.size(); i++) {
Log.v(TAG, "subscription : " + subscriptions.get(i).getId());
}
} else {
Log.d(TAG, "subscriptions: null");
mDetailTextView.setText("None");
}
}
}
它代替GetContacts
啓動了:
new GetSubscriptionTask().execute(mAccount);
你可以找到一個完整的例子here
非常感謝。我花了將近兩整天的時間試圖弄清楚。 – code511788465541441