question

alexey-shpilka10314 avatar image
alexey-shpilka10314 asked Tyler Liu commented

Can not change refresh_token

Hello, i use the library https://github.com/ringcentral/ringcentral-php.

$apiResponse = $rcsdk->platform()->post('/restapi/oauth/token', [
    'refresh_token' => $data['refresh_token'],
    'grant_type' => 'refresh_token',
    'endpoint_id' => $_ENV['RC_AppKey']

]);


I get a response from the server, Unsupported grant type. What is the problem?

errors
1 |3000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Phong Vu avatar image
Phong Vu answered alexey-shpilka10314 commented
Hi Alexey,

Why do you want to change the refresh token? After logged in, if the access token expired, you can call $rcsdk->platform()->refresh() to refresh the access token. If the refresh token also expired, you just need to call $rcsdk->platform()->login($option) again.

You can always check if the access token and the refresh token is still valid or not to decide what to do by calling $rcsdk->auth()->accessTokenValid() or $rcsdk->auth()->accessTokenValid(), respectively.

+ Phong
1 comment
1 |3000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

alexey-shpilka10314 avatar image alexey-shpilka10314 commented ·
We need automation of the process, is it possible to authorize without entering a username and password?
0 Likes 0 ·
John Wang avatar image
John Wang Deactivated answered alexey-shpilka10314 commented
Alexey,

Can you verify that your app has the "Refresh Access Token" flow grant which is required to refresh your token? I will look like the following in the "Settings" > "OAuth Settings" > "Authorization Flows" section of your app configuration as shown below:



Thanks,
John
1 comment
1 |3000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

alexey-shpilka10314 avatar image alexey-shpilka10314 commented ·

0 Likes 0 ·
Tyler Liu avatar image
Tyler Liu answered Tyler Liu commented
Instead of the code you post, could you please try '$rcsdk->platform()->refresh()' instead? Because the PHP SDK already provides method for refreshing.
37 comments
1 |3000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Tyler Liu avatar image Tyler Liu ♦ commented ·
Because you haven't got the token yet. So you cannot call refresh.

'->authUrl()' method return an URL, it doesn't do authorization at all.
1 Like 1 ·
alexey-shpilka10314 avatar image alexey-shpilka10314 commented ·
thanks, helped, I thought I was already authorized
1 Like 1 ·
Tyler Liu avatar image Tyler Liu ♦ commented ·
Login developer.ringcentral.com, go to your RingCentral app, navigate to Settings tab. And search for "Authorization Flows". There should be "Authorization code flow", otherwise your code won't work. 

You can create a new app and make it support Authorization code flow.
1 Like 1 ·
Tyler Liu avatar image Tyler Liu ♦ commented ·
I don't have the permission to delete either. I will turn to my colleagues for help. They live in different time zone so maybe they won't respond in several hours.

You should just change your password for security.

I will find some one to edit the password message but you have to wait.
1 Like 1 ·
alexey-shpilka10314 avatar image alexey-shpilka10314 commented ·

0 Likes 0 ·
alexey-shpilka10314 avatar image alexey-shpilka10314 commented ·
method out of the box, why it does not work?
0 Likes 0 ·
Tyler Liu avatar image Tyler Liu ♦ commented ·
'->login('username', 'extension (or leave blank)', 'password');' does authorization
0 Likes 0 ·
alexey-shpilka10314 avatar image alexey-shpilka10314 commented ·
what headings need to be passed to api, we write on the php curl? I have such problems though with a library, though with a self-written code?
0 Likes 0 ·
alexey-shpilka10314 avatar image alexey-shpilka10314 commented ·

0 Likes 0 ·
Tyler Liu avatar image Tyler Liu ♦ commented ·
Awesome! Feel free to post more questions and we are here to help :)
0 Likes 0 ·
alexey-shpilka10314 avatar image alexey-shpilka10314 commented ·
Another question is, we are not going to use your library, we need to form a curl request for php, perhaps you have an instruction how to do it?
0 Likes 0 ·
Tyler Liu avatar image Tyler Liu ♦ commented ·
Get token via curl:

curl -i -X POST "https://platform.devtest.ringcentral.com/restapi/oauth/token"; \
-H "Accept: application/json" \
-H "Content-Type: application/x-www-form-urlencoded" \
-u "clientId:clientSecret" \
-d "username=aaa&password=bbb&extension=&grant_type=password"

0 Likes 0 ·
Tyler Liu avatar image Tyler Liu ♦ commented ·
Refresh token is similar to get token.

For other api calls, please use  https://developer.ringcentral.com/api-explorer/latest/index.html

After you try it, it will show you the curl command.


0 Likes 0 ·
alexey-shpilka10314 avatar image alexey-shpilka10314 commented ·
an hour ago about authorized, now does not work
0 Likes 0 ·
alexey-shpilka10314 avatar image alexey-shpilka10314 commented ·
or it allows you to log in once?

0 Likes 0 ·
Tyler Liu avatar image Tyler Liu ♦ commented ·
You are trying authorization code flow instead of password flow. And it seems that your RingCentral app only supports Password flow.
0 Likes 0 ·
Tyler Liu avatar image Tyler Liu ♦ commented ·
For authorization code flow, please refer to this demo:  https://github.com/ringcentral/ringcentral-demos-oauth/tree/master/php
0 Likes 0 ·
alexey-shpilka10314 avatar image alexey-shpilka10314 commented ·
I chose the type Server only, but my query does not work.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://platform.devtest.ringcentral.com/restapi/oauth/token;");
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
curl_setopt($ch,CURLOPT_CUSTOMREQUEST,'POST');
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,false);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'username=+13127675868&password=k%agZJw*****&extension=101&grant_type=password');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_USERPWD, "J0yi-vxPQGCqqYW*****" . ":" . "adiZ7tdbTn6hX4Zps940PAF_qMRJKtTCS6vI******");

$headers = array();
$headers[] = "Accept: application/json";
$headers[] = "Content-Type: application/x-www-form-urlencoded";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}

curl_close ($ch);
echo '<pre>';
print_r($result);
die();


help with headers
0 Likes 0 ·
Tyler Liu avatar image Tyler Liu ♦ commented ·
It means clientId or clientSecret is wrong. If you just created a new RC app, you should use its clientId & clientSecret instead of the old app's.
0 Likes 0 ·
alexey-shpilka10314 avatar image alexey-shpilka10314 commented ·
there everything is correct, I checked, the error is the same
0 Likes 0 ·
Tyler Liu avatar image Tyler Liu ♦ commented ·
There shouldn't be a semicolon at the end of URL. I made a typo in my curl sample.

I have no experience with PHP-Curl. The curl sample I provided should work and I tested it multiple times.

Could you please print the headers you sent to the server? Let's see why it complains authorization header is invalid.
0 Likes 0 ·
Tyler Liu avatar image Tyler Liu ♦ commented ·
You can use something like  https://github.com/Runscope/requestbin#deploy-your-own-instance-using-docker to inspect the headers you sent.
0 Likes 0 ·
alexey-shpilka10314 avatar image alexey-shpilka10314 commented ·
$client_id = 'J0yi-vxPQGCqqYWwLVylBA';
$client_secret = 'adiZ7tdbTn6hX4Zps940PAF_qMRJKtTCS6vIozjVqlog';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://platform.devtest.ringcentral.com/restapi/oauth/token;");
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
curl_setopt($ch,CURLOPT_CUSTOMREQUEST,'POST');
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,false);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'username=+XXXXXXXX&password=XXXXXXXX&extension=101&grant_type=password');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_USERPWD, $client_id. ":".$client_secret);

$headers = array();
$headers[] = "Accept: application/json";
$headers[] = "Content-Type: application/x-www-form-urlencoded";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}

curl_close ($ch);
echo '
';
print_r($result);
0 Likes 0 ·
Tyler Liu avatar image Tyler Liu ♦ commented ·
0 Likes 0 ·
Tyler Liu avatar image Tyler Liu ♦ commented ·
After you fix the issue above, you will get this error:

<pre>{
                    
  "timestamp" : 1527244216326,
  "status" : 400,
  "error" : "Bad Request",
  "exception" : "org.eclipse.jetty.http.BadMessageException",
  "message" : "Unable to parse form content",
  "path" : "/authorization/v1/oauth/token"
}%

It is because there is special character in your password which curl dislikes.
0 Likes 0 ·
alexey-shpilka10314 avatar image alexey-shpilka10314 commented ·
Yes, I received this error, there was a misprint in the link when copied), thank you very much, I'm just shocked, I sat for the whole evening and could not understand the problem, why not make a password check and report this error
0 Likes 0 ·
alexey-shpilka10314 avatar image alexey-shpilka10314 commented ·
How do I delete a password message now?
0 Likes 0 ·
alexey-shpilka10314 avatar image alexey-shpilka10314 commented ·

0 Likes 0 ·
alexey-shpilka10314 avatar image alexey-shpilka10314 commented ·
again headers)
0 Likes 0 ·
alexey-shpilka10314 avatar image alexey-shpilka10314 commented ·
do I need to transfer in headers client_id and client secret?

0 Likes 0 ·
Tyler Liu avatar image Tyler Liu ♦ commented ·
Why don't you use the official PHP SDK instead? It handles headers for you automatically.  
0 Likes 0 ·
alexey-shpilka10314 avatar image alexey-shpilka10314 commented ·
I can not use php in general, now I'm writing on php to quickly rewrite it to zoho, which knows which headers and data to send.
0 Likes 0 ·
alexey-shpilka10314 avatar image alexey-shpilka10314 commented ·
I will write in the language of the zoho deluge
0 Likes 0 ·
alexey-shpilka10314 avatar image alexey-shpilka10314 commented ·
the last mistake I received, { "message" : "Unsupported Media Type", "errors" : [ ] }
0 Likes 0 ·
alexey-shpilka10314 avatar image alexey-shpilka10314 commented ·

0 Likes 0 ·
alexey-shpilka10314 avatar image alexey-shpilka10314 commented ·
I understood what the problem was not to use urlencoded
0 Likes 0 ·
Tyler Liu avatar image Tyler Liu ♦ commented ·
If I was you, I would choose Ruby instead of PHP for Proof of concept. You have to rewrite the code in zoho deluge anyway. So you just choose the language which could help you to get things done very quickly.

I am not telling you that Ruby is quicker to write than PHP. Because we have an official Ruby SDK which is 10 times simpler than the PHP SDK. (Disclaimer: I am the author of the Ruby SDK while I am not the author of the PHP SDK).

By reading the Ruby SDK's source code here, you can quickly figure out everything you need to know.   https://github.com/ringcentral/ringcentral-ruby/blob/master/lib/ringcentral.rb

For example: by reading  https://github.com/ringcentral/ringcentral-ruby/blob/master/lib/ringcentral.rb#L159  You will know that before you get the token, you should use basic header, and after you get the token you should use bearer token instead.
0 Likes 0 ·

Developer sandbox tools

Using the RingCentral Phone for Desktop, you can dial or receive test calls, send and receive test SMS or Fax messages in your sandbox environment.

Download RingCentral Phone for Desktop:

Tip: switch to the "sandbox mode" before logging in the app:

  • On MacOS: press "fn + command + f2" keys
  • On Windows: press "Ctrl + F2" keys