im trying to upload to my imgur Account's album, however, the upload works, but it looks like its gonna uploaded as anonym/public?
here's my code:
<?php
$client_id = "465xxx8c44294";
$image = file_get_contents("../images/d4487317c3xxx93210b293c2e.jpg");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.imgur.com/3/image.json');
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Client-ID ' . $client_id));
//curl_setopt($ch, CURLOPT_POSTFIELDS, array('image' => base64_encode($image)));
curl_setopt($ch, CURLOPT_POSTFIELDS, array('image' => base64_encode($image), 'album' => 'nqxxxGE'));
$reply = curl_exec($ch);
curl_close($ch);
$reply = json_decode($reply);
printf('<img height="180" src="%s" >', $reply->data->link);
any advice?
If you want to add image to album, according doc, you need to pass album id. Make sure you've generated token that has access to secret albums.
Here you can find some tips about tokens.
curl_setopt($ch, CURLOPT_POSTFIELDS,
array(
'image' => base64_encode($image),
'album' => '5' // 5 - your album id
)
);
You can check you albums id using this api.
To refresh token:
If a user has authorized their account but you no longer have a valid access_token for them, then a new one can be generated by using the refresh_token.
To obtain a new access token, your application performs a POST to https://api.imgur.com/oauth2/token. The request must include the following parameters to use a refresh token:
refresh_token: The refresh token returned from the authorization code exchange
client_id: The client_id obtained during application registration
client_secret: The client secret obtained during application registration.
grant_type: As defined in the OAuth2 specification, this field must contain a value of: refresh_token.
As long as the user has not revoked the access granted to your application, the response includes a new access token. A response from such a request is shown below:
{
"access_token":"5c3118ebb73fbb275945ab340be60b610a3216d6",
"refresh_token":"d36b474c95bb9ee54b992c7c34fffc2cc343d0a7",
"expires_in":3600,
"token_type":"Bearer",
"account_username":"saponifi3d"
}
Add the refresh part at the start of your script. Something like:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.imgur.com/oauth2/token');
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, [
'refresh_token' => $refreshToken, // Your refresh_token
'client_id' => $client_id,
'client_secret' => $clientSecret, //Your client_secret
'grant_type' => 'refresh_token'
]);
//Keep in mind that refreshToken and clientSecret are obtained during registration.
$reply = curl_exec($ch);
curl_close($ch);
$reply = json_decode($reply);
$accessToken = $reply->access_token;
Related
I seen answers for this in other posts but they are quite outdated and I know IG has updated their api since.
I followed their developer documentation but can not seem to make a request from my localhost.
$fields = array(
'client_id' => 'a2xxxxxxxxxxxxxxxxxxxxxxxxxxxxed',
'client_secret' => '3bxxxxxxxxxxxxxxxxxxxxxxxxxxxxx8f',
'grant_type' => 'authorization_code',
'redirect_uri' => 'http://localhost:8000/',
'code' => 'code'
);
$url = 'https://api.instagram.com/oauth/access_token';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
curl_setopt($ch, CURLOPT_POST,true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
$result = curl_exec($ch);
curl_close($ch);
print $result; //your token
The above code is how I am making my request but I get the following error.
{"code": 400, "error_type": "OAuthException", "error_message": "Matching code was not found or was already used."}
I have disabled implicit oAuth, the app set up is pretty basic, with all the settings going to my personal site for the time being as the app is in sandbox mode anyway.
redirect_uri should be pointing to a public URL address, not localhost.
Turns out, I just had to logout of my Instagram account each time I wanted to request a new token.
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}
Here is my php script RefreshToken.php
<?php
$url = 'https://accounts.google.com/o/oauth2/token';
$post_data = array(
'code' => 'xxxxxxxx',
'client_id' => 'xxxxxxxxxxx',
'client_secret' => 'xxxxxxxxxxxxx',
'redirect_uri' => 'http://localhost/googleapi/AuthenticationCode.php',
'grant_type' => 'authorization_code',
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$token = json_decode($result);
echo $token->refresh_token . "\n";
?>
Run PHP CLI
php -q RefreshToken.php
PHP Notice: Undefined property: stdClass::$refresh_token in /var/www/googleapi/RefreshToken.php on line 20
The refresh_token is not returned by default in Google OAuth2.
The request for an authorization code requires an extra parameter (access_type): then refresh token will be returned with access_token.
Other unusual behaviour: the refresh_token is returned only one time for a user. If, for some reason, refresh_token is lost for that user, then user will need to open Google Account Security Settings page and drop access for your app. And this is not enough: your app will need to pass more one params (approval_prompt equals true) to indicate we're forcing the request for a new refresh_token.
More info: https://developers.google.com/accounts/docs/OAuth2WebServer#offline)
My question relates to my previous question where I thought I had solved the issue. I've been able to obtain a refresh_token using scripts I found - including the ones in my previous question.
However now armed with this refresh token I am now trying to do some simple things in the first instance list the events in my test calendar
My problem is that trying to list the events using this URL returns an error - trying to get property of non object. This relates to this line of code:
$cAccessToken = $oToken->access_token;
So I assume there's something wrong in getting the access token.
In the part of my script where I obtained the code I needed to add this code:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
This produced the token- so I added this line to the code getting the access token again but it simply produced a blank page.
I must admit to be very confused with the google API. I tried using the google-client-api code but don't quite get this either - I keep running into brick walls.
Here's my code:
<?php
$cPublisherID = '';
$cScope = 'https://www.google.com/calendar/feeds/';
$cClientID = 'XXXXXX.apps.googleusercontent.com';
$cClientSecret = 'XXXXXX';
$cRedirectURI = 'XXXXXX';
$cAuthCode = 'XXXXXX';
$cRefreshToken = 'XXXXXX';
$bRefresh = true;
if (empty($cAuthCode)) {
$rsParams = array(
'response_type' => 'code',
'client_id' => $cClientID,
'redirect_uri' => $cRedirectURI,
'scope' => $cScope
);
$cOauthURL = 'https://accounts.google.com/o/oauth2/auth?' . http_build_query($rsParams);
echo("Go to\n$cOauthURL\nand enter the given value into \$cAuthCode\n");
exit();
} // ends if (empty($cAuthCode))
elseif (empty($cRefreshToken)) {
$cTokenURL = 'https://accounts.google.com/o/oauth2/token';
$rsPostData = array(
'code' => $cAuthCode,
'client_id' => $cClientID,
'client_secret' => $cClientSecret,
'redirect_uri' => $cRedirectURI,
'grant_type' => 'authorization_code',
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $cTokenURL);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $rsPostData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$cTokenReturn = curl_exec($ch);
$oToken = json_decode($cTokenReturn);
echo("Enter the following values:\n\n");
echo("\$cRefreshToken = '" . $oToken->refresh_token . "';\n");
} // ends
else {
// Get a new Access Token
$cTokenURL = 'https://accounts.google.com/o/oauth2/token';
$rsPostData = array(
'client_secret' => $cClientSecret,
'grant_type' => 'refresh_token',
'refresh_token' => $cRefreshToken,
'client_id' => $cClientID
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $cTokenURL);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $rsPostData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$cTokenReturn = curl_exec($ch);
$oToken = json_decode($cTokenReturn);
$cAccessToken = $oToken->access_token;
// Get the results
//$cGANURL = 'https://www.googleapis.com/gan/v1beta1/publishers/' . $cPublisherID . '/events';
$rest_url = 'https://www.googleapis.com/calendar/v3/calendars/primary/events';
$cAuthHeader = "Authorization: OAuth " . $cAccessToken;
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array($cAuthHeader));
curl_setopt($ch, CURLOPT_URL, $rest_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$cJsonReturn = curl_exec($ch);
print_r(json_decode($cJsonReturn));
} // ends else from <all authenticated>
?>
I really don't know enough to resolve this - so basically I think I'm getting the tokens I need including the refresh token but not sure how to test and understand what's going on.
I've used the oauth playground and can get results there but not with my own code. I used different codes by the way as I assumed they should be different?
I am trying to upload a photo to user's album with current script:
$this->facebook->api('/me/photos?access_token='.$user->access_token, 'post', $args);
$user->access_token is the user's access token and I check via Facebook Graph API Explorer tool it says still valid (until 2 months).
However if I create API request with script above, my console always returned:
Fatal error: Uncaught OAuthException: An active access token must be used to query information about the current user.
What I've been doing is:
deauthorize app
re-authorize app
request permission (publish_stream,photo_upload,user_photos)
update FB SDK to latest version
Here is the full code:
$user = $this->User_model->get_access_token($this->input->post('fb_id'));
foreach($this->input->post('media') as $media_id)
{
$media = $this->Media_model->get_media($media_id);
$args = array('message' => 'Photo Caption');
$args['image'] = '#' . realpath(FCPATH.'uploads/booth_1/'.$media->file);
$data = $this->facebook->api('/me/photos?access_token='.$user->access_token, 'post', $args);
print_r($data);
}
Any help?
FYI I am using codeigniter. And actually I was trying hours ago (it worked), however, now is not, because of access_token.
Thanks.
I solved it manually by NOT using built-in $facebook->api() but I am using curl instead.
$filename = '#' . realpath(FCPATH.'uploads/'.$media);
$url = "https://graph.facebook.com/".$user->fb_id."/photos";
$ch = curl_init($url);
$attachment = array('access_token' => $user->access_token,
'app_id' => APP_ID,
'name' => "Upload",
'fileUpload' => true,
'message' => "Message",
'image' => $filename,
);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_CAINFO, PATH TO FB SDK CERT FOR SSL);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
$result = curl_exec($ch);
if ($result === FALSE) {
die("Curl failed: " . curl_error($ch));
}
curl_close ($ch);