Reputation: 507
I am developing an application in which user can subscribe to various channels in my app itself. So to save api request i want to know whether user has already subscribed a youtube channel or not. While researching I have found some code and modified that to my requirements:
static public boolean checkIfUserAlreadySubscribed(String channelyoutubeid) {
SubscriptionListResponse response = null;
try {
HashMap<String, String> parameters = new HashMap<>();
parameters.put("part", "snippet,contentDetails");
parameters.put("forChannelId", channelyoutubeid);
parameters.put("mine", "true");
YouTube.Subscriptions.List subscriptionsListForChannelIdRequest = MainActivity
.mService.subscriptions().list(parameters.get("part").toString());
if (parameters.containsKey("forChannelId") && parameters.get("forChannelId") != "") {
subscriptionsListForChannelIdRequest.setForChannelId(parameters
.get("forChannelId").toString());
}
if (parameters.containsKey("mine") && parameters.get("mine") != "") {
boolean mine = (parameters.get("mine") == "true") ? true : false;
subscriptionsListForChannelIdRequest.setMine(mine);
}
response = subscriptionsListForChannelIdRequest.execute();
} catch (IOException e) {
e.printStackTrace();
}
if (response != null) {
//What should i do here
} else {
//whta should i pass
}
}
On executing my code response value is always not null whether user has subscribed or not .Can anyone suggest me what to do??
Upvotes: 1
Views: 666
Reputation: 8112
To retrieve channels which a user has already subscribed, you may want to try using Activities: list
with mine
parameter set to true
. A successful request will return response body with activity resource:
"contentDetails": {
"subscription": {
"resourceId": {
"kind": string,
"channelId": string,
}
}
}
contentDetails.subscription.resourceId.channelId
is the ID that YouTube uses to uniquely identify the channel that the user subscribed to.
Upvotes: 1