How to get access token using gmail api

前端 未结 7 1007
遇见更好的自我
遇见更好的自我 2021-01-01 22:29

I got the authorization code following this document. But when I tried to get access token, I always got errors. Can anyone help me ?

public String AccessTok         


        
7条回答
  •  情话喂你
    2021-01-01 23:05

    Refer : https://developers.google.com/android-publisher/authorization

    You already have authorization code that is called "refresh token". Please keep it in safe place. You can use "refresh token" to generate "access token".

    To get "access token", please make a post request to following URL

    https://accounts.google.com/o/oauth2/token

    Parameters:

    • grant_type
    • client_id
    • client_secret
    • refresh_token

    where "grant_type" should be "refresh_token"

    We are using PHP to do same, here is PHP's code for your reference

        $curl = curl_init();
    
        curl_setopt_array($curl, array(
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_URL => 'https://accounts.google.com/o/oauth2/token',
        CURLOPT_USERAGENT => 'Pocket Experts Services',
        CURLOPT_POST => 1,
        CURLOPT_POSTFIELDS => array(
        "grant_type" => "refresh_token",
        "client_id" => $GOOGLE_CLIENT_ID,
        "client_secret" => $GOOGLE_CLIENT_SECRET,
        "refresh_token" => $GOOGLE_REFRESH_TOKEN,
        )));
    
    
        // Send the request & save response to $resp
        $resp = curl_exec($curl);
    

    Hope it will help you.

提交回复
热议问题