Pull Youtube account info via access token? - php

I am working with OAuth for the first time and playing around with the Youtube one. I got the following code:
if(isset($_GET['code'])) {
$code = $_GET['code'];
$url = 'https://accounts.google.com/o/oauth2/token';
$params = array(
"code" => $code,
"client_id" => "XXX",
"client_secret" => "YYY",
"redirect_uri" => "URL",
"grant_type" => "authorization_code"
);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$json_response = curl_exec($curl);
curl_close($curl);
$authObj = json_decode($json_response);
echo "access_token: " . $authObj->access_token;
echo "token_type: " . $authObj->token_type;
echo "expires_in: " . $authObj->expires_in;
echo "refresh_token: " . $authObj->refresh_token;
$msg = '<p class="bg-success msg-padding"><b>Success:</b> You have successfully linked your Youtube account.</p>';
}
if(isset($_GET['error'])) {
$msg = '<p class="bg-danger msg-padding"><b>Error:</b> You have canceled the Youtube account linking process.</p>';
}
How do I obtain the Youtube channels ID, username, subscribers count and such via the access token, if possible at all?

You can use this access token to access other data endpoints in the Data API v3 by specifying mine=true as parameter.
For more info the channel list endpoint, check out the documentation: https://developers.google.com/youtube/v3/docs/channels/list

Related

DocuSign API Returns Un Authorized Client

I am getting 400 bad request with DocuSign demo account while accessing access token. I am using these values while making call.
$url = "https://account-d.docusign.com/oauth/token";
$integrator_and_secret_key = "Basic " . base64_encode("integration key:secret key");
$headers = [
"Authorization" => $integrator_and_secret_key,
"Content-Type" => "application/x-www-form-urlencoded",
];
$postData = [
"grant_type" => "authorization_code",
"code" => $_GET['code'],
];
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, "grant_type=authorization_code&code=authcode");
curl_setopt($curl, CURLOPT_HTTPHEADER,$headers
);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_VERBOSE, true);
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ( $status != 201 ) {
echo "error calling webservice, status is:" . $status . "\nerror text is --> ";
print_r($json_response); echo "\n";
exit(-1);
}
echo "<pre>"; print_r($json_response); exit;
$response = json_decode($json_response, true);
// if(isset($response["envelopeId"])){
// echo json_encode(array('output'=>'success'));
// }
// else{
// echo json_encode(array('status'=>False,'output'=>'Fail'));exit;
// }
exit;
There was an error calling "web service" after I called above, status: 400 error text -> error "error": "invalid_grant", "error_description": "unauthorized_client"} "
It looks like you're attempting to use the oauth authorization code grant flow.
The code that your q shows is the second major step where your app exchanges the authorization code it received from DocuSign for an access token that will be used to call the API.
Your code appears to be using the static string authcode as the authorization code:
curl_setopt($curl, CURLOPT_POSTFIELDS,
"grant_type=authorization_code&code=authcode");
This is a bug. The code body parameter must be set to the value of the authorization code that was previously received from DocuSign.
Eg
curl_setopt($curl, CURLOPT_POSTFIELDS,
"grant_type=authorization_code&code=$authcode");
Recommendation
Instead of hand-coding the authorization code grant, it is better to use a library. For PHP, the open source league/oauth2-client can be used. Docs
The DocuSign code example shows how to use it. See the Auth directory and its callers.

salesforce rest api invalid session id error when using access token

I am trying to retrieve data from salesforce using the REST api and CURL in PHP.
I perform the authentication request and recieve an 'instance_url' and 'access_token' but after performing a query request using those, i receive an "INVALID_SESSION_ID" error.
my authentication request code:
function get_sf_auth_data() {
$post_data = array(
'grant_type' => 'password',
'client_id' => 'xxxxxxxxxxxxxxxxxxxxxx', //My client id (xxx... for this example)
'client_secret' => '111111111111', // My client secret (111... for this example)
'username' => 'my_user_name',
'password' => 'my_user_password'
);
$headers = array(
'Content-type' => 'application/x-www-form-urlencoded;charset=UTF-8'
);
$curl = curl_init('https://login.salesforce.com/services/oauth2/token');
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
$response = curl_exec($curl);
curl_close($curl);
// Retrieve and parse response body
$sf_access_data = json_decode($response, true);
echo '*********' . $sf_response_data['instance_url'] . '********';
echo '*********' . $sf_response_data['access_token'] . '********';
return $sf_access_data;
}
My query request code:
function get_user_sfid($sf_access_data, $user_id_number){
$sql = "SELECT Id, Name
FROM Account
WHERE ID__c = '$user_id_number'";
$url = $sf_access_data['instance_url'] . '/services/data/v20.0/query/?q=' . urlencode($sql);
$headers = array(
'Authorization' => 'OAuth ' . $sf_access_data['access_token']
);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$json_response = curl_exec($curl);
curl_close($curl);
var_dump($json_response); // This prints out the response where i got the error
$response = json_decode($json_response);
return $response['id'];
}
The response to the query request as is:
[{"message":"Session expired or invalid","errorCode":"INVALID_SESSION_ID"}]
I have also tried using this guide (http://developer.force.com/cookbook/recipe/interact-with-the-forcecom-rest-api-from-php) as reference but it uses an "authorization_code" authentication and not a password one.
EDIT
The app has full access in salesforce and both api version and authentication data are correct
I found the solution.
I defined my headers array as:
$headers = array(
"Authorization" => "OAuth " . $sf_access_data['access_token']
);
When i should have defined it as:
$headers = array(
"Authorization: OAuth " . $sf_access_data['access_token']
);

Twitch get access token

I'm using the following PHP class which I found on GitHub: Twitch oauth class
So I've been creating the Authentification site, which will let you authentificate your twitch account and redirect you to another site using the following url:
http://website.com/twitch/dashboard.php?code=xxxxxxx&scope=user_read+channel_read+chat_login+user_follows_edit+channel_editor+channel_commercial+channel_check_subscription
Now I've used the following code to receive the access_token:
<?php
$ttv_code = $_GET['code'];
$access_token = $twitchtv->get_access_token($ttv_code);
$user_name = $twitchtv->authenticated_user($access_token);
if(isset($user_name)) {
echo 'Thank you ' . $user_name . '! Authentication Completed!';
}else{
echo 'Authentification failed!';
}
?>
But sadly there's no response coming back from the function. The function looks like the following:
function get_access_token($code) {
$ch = curl_init($this->base_url . "oauth2/token");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POST, 1);
$fields = array(
'client_id' => $this->client_id,
'client_secret' => $this->client_secret,
'grant_type' => 'authorization_code',
'redirect_uri' => $this->redirect_url,
'code' => $code
);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
$data = curl_exec($ch);
$response = json_decode($data, true);
return $response["access_token"];
}
Does anyone have a idea what causes this mistake? I would appreciate any kind of help.

How can I use Youtube API's access token to get the list of user's uploded video

I have already get the access token of user via Oauth2, so after authenticate user to my Project and ask them to manage their Youtube data i am storing access token to the DB
Now i want to get the detail or list of the video that uploaded by that authenticated user, so How can I use user's access token to get their Youtube video data?
Below is my code
I generate the Authentication Url like
$url = "https://accounts.google.com/o/oauth2/auth";
$params = array(
"response_type" => "code",
"client_id" => "XXXXXXX",
"redirect_uri" => "http://youtube-get.dev/oauth2callback.php",
"scope" => "https://gdata.youtube.com",
"access_type" => "offline"
);
$request_to = $url . '?' . http_build_query($params);
header("Location: " . $request_to);
And by calling curl in oauth2callback.php I get the Access Token
$client_id="XXXXXXX";
$client_secret="XXXXXX";
$redirect_uri="http://youtube-get.dev/oauth2callback.php";
$oauth2token_url = "https://accounts.google.com/o/oauth2/token";
$clienttoken_post = array(
"code" => $code,
"client_id" => $client_id,
"client_secret" => $client_secret,
"redirect_uri" => $redirect_uri,
"grant_type" => "authorization_code"
);
$curl = curl_init($oauth2token_url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $clienttoken_post);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$json_response = curl_exec($curl);
curl_close($curl);
$authObj = json_decode($json_response);
if (isset($authObj->refresh_token)){
//refresh token only granted on first authorization for offline access
//save to db for future use (db saving not included in example)
global $refreshToken;
$refreshToken = $authObj->refresh_token;
}
$accessToken = $authObj->access_token;
After getting this Access token how can i use it to get authenticated user's video list?
All the help will be appreciated, already wasted much time for it.
after authentication use below url to get authenticate users video
https://www.googleapis.com/youtube/v3/search?part=snippet&forMine=true&maxResults=25&q={your searchparameter}&type=video&key={your App Key}

Quickblox Token missing error PHP

I am trying to create and retrieve users programatically through quickblox. So far I have done authentication and session creation. But when after this step I try to retrieve users I get token is required error.
Here is the code:
DEFINE('APPLICATION_ID', 16619);
DEFINE('AUTH_KEY', "W4EFMSZx3nQZ7eh");
DEFINE('AUTH_SECRET', "NCp5PyZM9uQvWg4");
// User credentials
DEFINE('USER_LOGIN', "removed");
DEFINE('USER_PASSWORD', "removed");
// Quickblox endpoints
DEFINE('QB_API_ENDPOINT', "https://api.quickblox.com");
DEFINE('QB_PATH_SESSION', "session.json");
DEFINE('users', "users.json");
// Generate signature
$nonce = rand();
$timestamp = time(); // time() method must return current timestamp in UTC but seems like hi is return timestamp in current time zone
$signature_string = "application_id=".APPLICATION_ID."&auth_key=".AUTH_KEY."&nonce=".$nonce."&timestamp=".$timestamp."&user[login]=".USER_LOGIN."&user[password]=".USER_PASSWORD;
echo "stringForSignature: " . $signature_string . "<br><br>";
$signature = hash_hmac('sha1', $signature_string , AUTH_SECRET);
// Build post body
$post_body = http_build_query(array(
'application_id' => APPLICATION_ID,
'auth_key' => AUTH_KEY,
'timestamp' => $timestamp,
'nonce' => $nonce,
'signature' => $signature,
'user[login]' => USER_LOGIN,
'user[password]' => USER_PASSWORD
));
// Configure cURL
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, QB_API_ENDPOINT . '/' . QB_PATH_SESSION); // Full path is - https://api.quickblox.com/session.json
curl_setopt($curl, CURLOPT_POST, true); // Use POST
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_body); // Setup post body
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // Receive server response
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
// Execute request and read response
$response = curl_exec($curl);
// Check errors
if ($response) {
$cu = curl_init();
curl_setopt($cu, CURLOPT_URL, QB_API_ENDPOINT . '/' . users);
curl_setopt($cu, CURLOPT_RETURNTRANSFER, true);
curl_setopt($cu, CURLOPT_SSL_VERIFYPEER, FALSE);
$res = curl_exec($cu);
echo $res;
}
I am new to json and curl therefore any help will be highly appreciated
Session creation request returns a session token which you must use in each further request.
Just set the token and it will work

Categories