Drive, Gmail gibi pek çok Google hizmeti, kullanıcıların bu hizmetlerdeki verileriyle çalışmasına yardımcı olan uygulamalar oluşturmak için kullanabileceğiniz herkese açık API'ler sağlar. Bu hizmetlere erişmek için kullanıcıların kullanıcılardan izin almak ve API'lere erişim izni veren erişim jetonlarını almak üzere OAuth 2.0 istemci akışlarından birini uygulaması gerekir.
Oturum açmış olan kullanıcıya erişim jetonları almak için sizin adınıza OAuth 2.0 akışını uygulayan Google Oturum Açma kitaplığını kullanabilirsiniz.
Başlamadan önce
Temel Google ile Oturum Açma entegrasyonunu tamamlamanız gerekir.
1. Hangi kapsamların verildiğini kontrol edin
Google API'ye çağrı yapmadan önce, GIDGoogleUser
kapsamındaki grantedScopes
özelliğini kullanarak uygulamanıza hangi kapsamların verildiğini kontrol edin:
Swift
let driveScope = "https://www.googleapis.com/auth/drive.readonly"
let grantedScopes = user.grantedScopes
if grantedScopes == nil || !grantedScopes!.contains(driveScope) {
// Request additional Drive scope.
}
Objective-C
NSString *driveScope = @"https://www.googleapis.com/auth/drive.readonly";
// Check if the user has granted the Drive scope
if (![user.grantedScopes containsObject:driveScope]) {
// request additional drive scope
}
Belirli bir kapsam için kullanıcı tarafından izin verilip verilmediğine bağlı olarak, belirli bir etkileşimi desteklemek amacıyla ek kapsam için istekte bulunmanız gerekebilir.
2. Ek kapsam isteme
Ek kapsam istemeniz gerekiyorsa addScopes:presentingViewController:completion
veya addScopes:presentingWindow:completion
numaralı telefonu arayarak kullanıcıdan uygulamanıza ek erişim izni vermesini isteyin.
Örneğin, bir kullanıcının Drive dosyalarına salt okuma erişimi istemek için:
Swift
let additionalScopes = ["https://www.googleapis.com/auth/drive.readonly"]
guard let currentUser = GIDSignIn.sharedInstance.currentUser else {
return ; /* Not signed in. */
}
currentUser.addScopes(additionalScopes, presenting: self) { signInResult, error in
guard error == nil else { return }
guard let signInResult = signInResult else { return }
// Check if the user granted access to the scopes you requested.
}
Objective-C
NSArray *additionalScopes = @[ @"https://www.googleapis.com/auth/drive.readonly" ];
GIDGoogleUser *currentUser = GIDSignIn.sharedInstance.currentUser;
[currentUser addScopes:additionalScopes
presentingViewController:self
completion:^(GIDSignInResult * _Nullable signInResult,
NSError * _Nullable error) {
if (error) { return; }
if (signInResult == nil) { return; }
// Check if the user granted access to the scopes you requested.
}];
3. Yeni jetonlarla API çağrısı yapma
Google API çağrılarınıza her zaman süresi dolmuş erişim jetonları eklendiğinden emin olmak için çağrıları bir refreshTokensIfNeededWithCompletion:
blokunda sarmalayın:
Swift
currentUser.refreshTokensIfNeeded { user, error in
guard error == nil else { return }
guard let user = user else { return }
// Get the access token to attach it to a REST or gRPC request.
let accessToken = user.accessToken.tokenString
// Or, get an object that conforms to GTMFetcherAuthorizationProtocol for
// use with GTMAppAuth and the Google APIs client library.
let authorizer = user.fetcherAuthorizer()
}
Objective-C
[currentUser refreshTokensIfNeededWithCompletion:^(
GIDGoogleUser * _Nullable user,
NSError * _Nullable error) {
if (error) { return; }
if (user == nil) { return; }
// Get the access token to attach it to a REST or gRPC request.
NSString *accessToken = user.accessToken.tokenString;
// Or, get an object that conforms to GTMFetcherAuthorizationProtocol for
// use with GTMAppAuth and the Google APIs client library.
id<GTMFetcherAuthorizationProtocol> authorizer = [user fetcherAuthorizer];
}];
Erişim jetonunu bir REST veya gTB isteğinin (Authorization: Bearer ACCESS_TOKEN
) başlığına ekleyerek ya da Google API'leri İstemci Kitaplığı ile alıcı yetkilendirme aracını kullanarak API'yi çağırmak için erişim jetonunu kullanın.