2

我正在尝试在学校项目中实施 Google Fit。我正在使用 Google 发布的文档,但它不起作用。我收到 403 bad request error 是否可能是因为我正在使用 XAMPP 尝试连接,它给了我一个错误?我已经在 Google 项目中输入了请求域。

你能帮助我吗?我还有一个问题:使用 Google Fit Api rest 是否需要付费?谢谢大家的回答

var CLIENT_SECRET;
var CLIENT_ID;

$.post('healthDataFunc.php', { functionname: 'get_app_credentials' }, function(data){
    var result = JSON.parse(data);
    if (result){
        CLIENT_SECRET = result[0];
        CLIENT_ID = result[1];
    }
});

var CLIENT_REDIRECT = "https://localdefault.com:4433";
//End-Configuration

var SCOPES_FITNESS = 'https://www.googleapis.com/auth/fitness.activity.read+https://www.googleapis.com/auth/fitness.body.read+https://www.googleapis.com/auth/fitness.location.read+https://www.googleapis.com/auth/fitness.blood_pressure.read+https://www.googleapis.com/auth/fitness.sleep.read';
var GoogleAuth;

/*
    Initial request for Google authentication code
    Opens google auth window
    returns Google Auth token
*/

function requestGoogleoAuthCode() {
    // Load the API's client and auth2 modules.
    // Call the initClient function after the modules load.
    gapi.load('client:auth2', initClient);
    console.log(gapi);
}

function initClient() {    
    // In practice, your app can retrieve one or more discovery documents.
    var discoveryUrl = "https://www.googleapis.com/discovery/v1/apis/drive/v3/rest";

    // Initialize the gapi.client object, which app uses to make API requests.
    // Get API key and client ID from API Console.
    // 'scope' field specifies space-delimited list of access scopes.
    gapi.client.init({
        'apiKey': CLIENT_SECRET,
        'clientId': CLIENT_ID,
        'discoveryDocs': [discoveryUrl],
        'scope': SCOPES_FITNESS
    }).then(function () {
        GoogleAuth = gapi.auth2.getAuthInstance();

        // Listen for sign-in state changes.
        GoogleAuth.isSignedIn.listen(updateSigninStatus);

        // Handle initial sign-in state. (Determine if user is already signed in.)
        var user = GoogleAuth.currentUser.get();

        GoogleAuth.signIn();

        console.log("test--------->", GoogleAuth, user);

    });
}


/*
    Uses Google Auth code to get Access Token and Refresh Token
    returns object with access token, refresh token and access token expiration
*/
function getAccessToken(google_auth_code) {
    var retVal = null;
    jQuery.ajax({
        url: "https://www.googleapis.com/oauth2/v3/token?code=" + google_auth_code + "&redirect_uri=" + CLIENT_REDIRECT + "&client_id=" + CLIENT_ID + "&client_secret=" + CLIENT_SECRET + "&scope=&grant_type=authorization_code",
        type: "post",
        success: function (result) {
            console.log("Got Access Token And Refresh Token");
            retVal = result;
            console.log(result);
        },
        error: function (jqXHR, textStatus, errorThrown) {
            console.log("Error during getAccessToken");
            console.log(jqXHR);
            console.log(textStatus);
            console.log(errorThrown);
            retVal = null;
        },
        async: false
    });
    return retVal;
}


/*
    Uses Refresh token to obtain new access token
    returns new access token with expiration
*/
function refreshAccessToken(refresh_token) {
    var retVal = null;
    jQuery.ajax({
        url: "https://www.googleapis.com/oauth2/v3/token?client_secret=" + CLIENT_SECRET + "&grant_type=refresh_token&refresh_token=" + refresh_token + "&client_id=" + CLIENT_ID,
        type: "post",
        success: function (result) {
            console.log("Refreshed Access Token");
            retVal = result;
        },
        error: function (jqXHR, textStatus, errorThrown) {
            console.log("Error during refreshAccessToken");
            console.log(jqXHR);
            console.log(textStatus);
            console.log(errorThrown);
            retVal = null;
        },
        async: false
    });
    return retVal;
}

function revokeAccess(accessToken) {
    GoogleAuth.disconnect();
}

var isAuthorized;
var currentApiRequest;

/**
 * Store the request details. Then check to determine whether the user
 * has authorized the application.
 *   - If the user has granted access, make the API request.
 *   - If the user has not granted access, initiate the sign-in flow.
 */
function sendAuthorizedApiRequest(requestDetails) {
  currentApiRequest = requestDetails;
  if (isAuthorized) {
    // Make API request
    // gapi.client.request(requestDetails)

    // Reset currentApiRequest variable.
    currentApiRequest = {};
  } else {
    GoogleAuth.signIn();
  }
}

/**
 * Listener called when user completes auth flow. If the currentApiRequest
 * variable is set, then the user was prompted to authorize the application
 * before the request executed. In that case, proceed with that API request.
 */
function updateSigninStatus(isSignedIn) {
  if (isSignedIn) {
    isAuthorized = true;
    if (currentApiRequest) {
      sendAuthorizedApiRequest(currentApiRequest);
    }
  } else {
    isAuthorized = false;
  }
}
4

0 回答 0