/// <summary> /// Instantiates the OAuth2 flow to retrieve authorization code for google cloud storage, and schedules /// invocation of the code handler on the received authorization code once it is available. Throws an exception /// once there is a failure to get the authorization code from the oauth2 flow. /// </summary> /// <param name="onAuthorizationCodeAction">An action to invoke on the authorization code instance when it is /// available.</param> private static void GetAuthCode(Action <AuthorizationCode> onAuthorizationCodeAction) { var server = new OAuth2Server(authorizationResponse => { _authorizationResponse = authorizationResponse; }); server.Start(); var redirectUri = server.CallbackEndpoint; _onOAuthResponseReceived = authorizationResponse => { if (!string.Equals("code", authorizationResponse.Key)) { throw new Exception("Could not receive required permissions"); } var authCode = new AuthorizationCode { Code = authorizationResponse.Value, RedirectUri = redirectUri }; if (onAuthorizationCodeAction != null) { onAuthorizationCodeAction(authCode); } }; // Take the user to the authorization page to authorize the application. var credentials = OAuth2Credentials.GetCredentials(); var authorizationUrl = string.Format("{0}?scope={1}&access_type=offline&redirect_uri={2}&response_type=code&client_id={3}", credentials.auth_uri, CloudStorageFullControlScope, redirectUri, credentials.client_id); Application.OpenURL(authorizationUrl); }
/// <summary> /// Sends an HTTP request to OAuth2 token uri to retrieve, process and store needed tokens. /// </summary> /// <param name="grantDictionary">A dictionary containing OAuth2 grant type and grant values to be used when /// requesting access token. The dictionary will be mutated by adding more credentials values needed to /// send the token request.</param> /// <param name="postTokenAction"></param> private static void RequestToken(Dictionary <string, string> grantDictionary, Action postTokenAction) { var credentials = OAuth2Credentials.GetCredentials(); grantDictionary.Add("client_id", credentials.client_id); grantDictionary.Add("client_secret", credentials.client_secret); WwwRequestInProgress.TrackProgress( HttpRequestHelper.SendHttpPostRequest(credentials.token_uri, grantDictionary, null), "Requesting access token", completeTokenRequest => { HandleTokenResponse(completeTokenRequest); postTokenAction(); }); }
/// <summary> /// Sends an HTTP request to GCP to create a bucket with the configured name, and invokes the response /// handler on the HTTP response. /// </summary> /// <param name="onCreateBucketResponseAction">An action to be invoked on the www instance holding the HTTP request /// once the response to the request to create bucket is available.</param> private static void CreateBucket(Action <WWW> onCreateBucketResponseAction) { var credentials = OAuth2Credentials.GetCredentials(); // see https://cloud.google.com/storage/docs/creating-buckets on creating buckets. var createBucketEndPoint = string.Format("https://www.googleapis.com/storage/v1/b?project={0}", credentials.project_id); var createBucketRequest = new CreateBucketRequest { name = QuickDeployWindow.Config.CloudStorageBucketName }; var jsonBytes = Encoding.UTF8.GetBytes(JsonUtility.ToJson(createBucketRequest)); var createBucketWww = SendAuthenticatedPostRequest(createBucketEndPoint, jsonBytes, "application/json"); WwwRequestInProgress.TrackProgress( createBucketWww, string.Format("Creating bucket with name \"{0}\"", createBucketRequest.name), onCreateBucketResponseAction); }