From bb195dfecc8ac85e11930d142d8679735c5ee6b2 Mon Sep 17 00:00:00 2001 From: Dakshi Date: Wed, 12 Jul 2023 07:44:35 +0530 Subject: [PATCH 1/8] added providerAccessToken section --- app/views/docs/oauth-providers/github.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app/views/docs/oauth-providers/github.md b/app/views/docs/oauth-providers/github.md index cd4764173..f97074302 100644 --- a/app/views/docs/oauth-providers/github.md +++ b/app/views/docs/oauth-providers/github.md @@ -346,4 +346,7 @@ let account = Account(client) let session = try await account.updateSession( sessionId: "current" ) -``` \ No newline at end of file +``` +## Accessing the Provider Access Token + +After the user has been authorized and redirected back to your app, you can find their GitHub provider access token in the [session object](https://appwrite.io/docs/models/session) by accessing the `providerAccessToken` property. This access token can be used to authenticate the user and make API requests on their behalf. \ No newline at end of file From 8a4b7fa95e86ae791de901d1d7b78d8f293ef79e Mon Sep 17 00:00:00 2001 From: Dakshi Date: Wed, 12 Jul 2023 07:54:28 +0530 Subject: [PATCH 2/8] added providerAccessToken section --- app/views/docs/oauth-providers/github.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/docs/oauth-providers/github.md b/app/views/docs/oauth-providers/github.md index f97074302..7c540b81c 100644 --- a/app/views/docs/oauth-providers/github.md +++ b/app/views/docs/oauth-providers/github.md @@ -349,4 +349,4 @@ let session = try await account.updateSession( ``` ## Accessing the Provider Access Token -After the user has been authorized and redirected back to your app, you can find their GitHub provider access token in the [session object](https://appwrite.io/docs/models/session) by accessing the `providerAccessToken` property. This access token can be used to authenticate the user and make API requests on their behalf. \ No newline at end of file +After the user has been authorized and redirected back to your app, you can find their GitHub provider access token in the [session object](https://appwrite.io/docs/models/session) by accessing the `providerAccessToken` property. This access token can be used to make requests to the GitHub API on their behalf. \ No newline at end of file From e1b23445e69f3504561cf8aef65287af57ed5e34 Mon Sep 17 00:00:00 2001 From: Dakshi Date: Sun, 16 Jul 2023 20:04:54 +0530 Subject: [PATCH 3/8] added code examples --- app/views/docs/oauth-providers/github.md | 219 ++++++++++++++++++++++- 1 file changed, 216 insertions(+), 3 deletions(-) diff --git a/app/views/docs/oauth-providers/github.md b/app/views/docs/oauth-providers/github.md index 7c540b81c..70014bb2b 100644 --- a/app/views/docs/oauth-providers/github.md +++ b/app/views/docs/oauth-providers/github.md @@ -226,7 +226,7 @@ func scene(_ scene: UIScene, openURLContexts URLContexts: Set) } ``` -To authenticate a user in your Android application, use the [Create OAuth2 Session](https://appwrite.io/docs/client/account?sdk=apple-default#accountCreateOAuth2Session) endpoint. +To authenticate a user in your Apple application, use the [Create OAuth2 Session](https://appwrite.io/docs/client/account?sdk=apple-default#accountCreateOAuth2Session) endpoint. ```swift import Appwrite @@ -347,6 +347,219 @@ let session = try await account.updateSession( sessionId: "current" ) ``` -## Accessing the Provider Access Token +## Provider Access Token -After the user has been authorized and redirected back to your app, you can find their GitHub provider access token in the [session object](https://appwrite.io/docs/models/session) by accessing the `providerAccessToken` property. This access token can be used to make requests to the GitHub API on their behalf. \ No newline at end of file +Provider access token is an authentication credential issued by GitHub once the user is successfully authenticated with GitHub. This access token allows you to make requests to the [GitHub API](https://docs.github.com/en/apps/oauth-apps/building-oauth-apps/authorizing-oauth-apps#3-use-the-access-token-to-access-the-api) on behalf of the user. + +You can easily retrieve the provider access token of the authenticated user, by accessing the `providerAccessToken` property within the [session object](https://appwrite.io/docs/models/session). + +### Web +To get this access token in your web application, use the [`account.getSession()`](https://appwrite.io/docs/client/account?sdk=web-default#accountGetSession) endpoint. +```javascript +import { Client, Account } from "appwrite"; + +const client = new Client(); + +const account = new Account(client); + +client + .setEndpoint("https://cloud.appwrite.io/v1") // Replace with your API endpoint + .setProject("[PROJECT_ID]"); // Replace with your project ID + +const promise = account.getSession("current"); + +promise.then( + function (response) { + const providerAccessToken = response.providerAccessToken; + + // Example Request to GitHub API + fetch("https://api.github.com/user", { + headers: { + Authorization: `Bearer ${providerAccessToken}`, + }, + }) + .then(function (response) { + return response.json(); + }) + .then(function (data) { + console.log(data); // GitHub API response data + }) + .catch(function (error) { + console.error(error); // Error occurred while calling GitHub API + }); + } +); +``` +### Flutter (Dart) +To get this access token in your flutter application, use the [`account.getSession()`](https://appwrite.io/docs/client/account?sdk=flutter-default#accountGetSession) endpoint. +```dart +import 'package:appwrite/appwrite.dart'; +import 'package:http/http.dart' as http; + +void main() async { + var client = Client(); + var account = Account(client); + + client + .setEndpoint('https://cloud.appwrite.io/v1') // Replace with your API endpoint + .setProject('[PROJECT_ID]') // Replace with your project ID + ; + + Future result = account.getSession( + sessionId: 'current', + ); + + result.then((response) { + var providerAccessToken = response.providerAccessToken; + + // Example Request to GitHub API + http.get(Uri.parse('https://api.github.com/user'), headers: { + 'Authorization': 'Bearer $providerAccessToken', + }).then((response) { + print(response.body); // GitHub API response data + }).catchError((error) { + print(error); // Error occurred while calling GitHub API + }); + }); +} +``` + +### Android (Kotlin) + +If you are using `OkHttp` library for making HTTP requests, you need to add the necessary dependencies in your project's `build.gradle` file. Here's an example of how to add the `OkHttp` dependency: + +```groovy +dependencies { + implementation 'com.squareup.okhttp3:okhttp:4.9.3' +} +``` +To get this access token in your Android application, use the [`account.getSession()`](https://appwrite.io/docs/client/account?sdk=android-kotlin#accountGetSession) endpoint. +```kotlin +import io.appwrite.Client +import io.appwrite.services.Account +import okhttp3.OkHttpClient +import okhttp3.Request +import java.io.IOException + +val client = Client() + .setEndpoint("https://cloud.appwrite.io/v1") // Replace with your API endpoint + .setProject("[PROJECT_ID]") // Replace with your project ID + +val account = Account(client) + +val response = account.getSession("current") +val providerAccessToken = response.body?.providerAccessToken ?: "" + +// Example Request to GitHub API +val httpClient = OkHttpClient() +val request = Request.Builder() + .url("https://api.github.com/user") // GitHub API endpoint + .header("Authorization", "Bearer $providerAccessToken") + .build() + +httpClient.newCall(request).execute().use { response -> + if (!response.isSuccessful) { + throw IOException("Unexpected code $response") // Error occurred while calling GitHub API + } + println(response.body?.string()) // GitHub API response data +} +``` + +### Android (Java) + + +If you are using `OkHttp` library for making HTTP requests, you need to add the necessary dependencies in your project's `build.gradle` file. Here's an example of how to add the `OkHttp` dependency: + +```groovy +dependencies { + implementation 'com.squareup.okhttp3:okhttp:4.9.3' +} +``` +To get this access token in your Android application, use the [`account.getSession()`](https://appwrite.io/docs/client/account?sdk=android-java#accountGetSession) endpoint. +```java +import io.appwrite.Client; +import io.appwrite.services.Account; +import io.appwrite.coroutines.CoroutineCallback; +import okhttp3.Call; +import okhttp3.Callback; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; +import java.io.IOException; + +Client client = new Client(getApplicationContext()) + .setEndpoint("https://cloud.appwrite.io/v1") // Replace with your API endpoint + .setProject("[PROJECT_ID]"); // Replace with your project ID + +Account account = new Account(client); + +account.getSession( + "current", + new CoroutineCallback<>((result, error) -> { + if (error != null) { + error.printStackTrace(); + return; + } + + String providerAccessToken = result.getProviderAccessToken(); + + // Example Request to GitHub API + OkHttpClient httpClient = new OkHttpClient(); + Request request = new Request.Builder() + .url("https://api.github.com/user") + .header("Authorization", "Bearer " + providerAccessToken) + .build(); + + httpClient.newCall(request).enqueue(new Callback() { + @Override + public void onResponse(Call call, Response response) throws IOException { + String responseBody = response.body().string(); + Log.d("Appwrite", responseBody); // GitHub API response data + } + + @Override + public void onFailure(Call call, IOException e) { + e.printStackTrace(); + } + }); + }) +); +``` + + +### iOS (Swift) +To get this access token in your Apple application, use the [`account.getSession()`](https://appwrite.io/docs/client/account?sdk=apple-default#accountGetSession) endpoint. +```swift +import Appwrite + +let client = Client() + .setEndpoint("https://cloud.appwrite.io/v1") // Replace with your API endpoint + .setProject("[PROJECT_ID]") // Replace with your project ID + +let account = Account(client) + +async { + do { + let session = try await account.getSession( + sessionId: "current" + ) + + let providerAccessToken = session.providerAccessToken + + // Example Request to GitHub API + let url = URL(string: "https://api.github.com/user")! + var request = URLRequest(url: url) + request.setValue("Bearer \(providerAccessToken)", forHTTPHeaderField: "Authorization") + + let (data, _) = try await URLSession.shared.data(for: request) + + if let data = data { + if let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] { + print(json) // GitHub API response data + } + } + } catch { + print(error) // Error occurred while calling GitHub API + } +} +``` From e60ae77bf44944a6dd27381b1d2a0a7c0ed7ea97 Mon Sep 17 00:00:00 2001 From: Dakshi Date: Tue, 18 Jul 2023 23:07:40 +0530 Subject: [PATCH 4/8] fixed kotlin and java code --- app/views/docs/oauth-providers/github.md | 134 ++++++++++++++--------- 1 file changed, 83 insertions(+), 51 deletions(-) diff --git a/app/views/docs/oauth-providers/github.md b/app/views/docs/oauth-providers/github.md index 70014bb2b..dac1f0a80 100644 --- a/app/views/docs/oauth-providers/github.md +++ b/app/views/docs/oauth-providers/github.md @@ -102,7 +102,7 @@ void main() async { // OAuth Login, for simplest implementation you can leave both success and // failure link empty so that Appwrite handles everything. - await account.createOAuth2Session('github'); + await account.createOAuth2Session(provider: 'github'); } ``` @@ -370,6 +370,7 @@ const promise = account.getSession("current"); promise.then( function (response) { + // Get the provider access token const providerAccessToken = response.providerAccessToken; // Example Request to GitHub API @@ -410,6 +411,7 @@ void main() async { ); result.then((response) { + // Get the provider access token var providerAccessToken = response.providerAccessToken; // Example Request to GitHub API @@ -434,40 +436,55 @@ dependencies { } ``` To get this access token in your Android application, use the [`account.getSession()`](https://appwrite.io/docs/client/account?sdk=android-kotlin#accountGetSession) endpoint. + ```kotlin +import android.os.Bundle +import androidx.activity.ComponentActivity import io.appwrite.Client import io.appwrite.services.Account +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch import okhttp3.OkHttpClient import okhttp3.Request import java.io.IOException -val client = Client() - .setEndpoint("https://cloud.appwrite.io/v1") // Replace with your API endpoint - .setProject("[PROJECT_ID]") // Replace with your project ID - -val account = Account(client) - -val response = account.getSession("current") -val providerAccessToken = response.body?.providerAccessToken ?: "" - -// Example Request to GitHub API -val httpClient = OkHttpClient() -val request = Request.Builder() - .url("https://api.github.com/user") // GitHub API endpoint - .header("Authorization", "Bearer $providerAccessToken") - .build() - -httpClient.newCall(request).execute().use { response -> - if (!response.isSuccessful) { - throw IOException("Unexpected code $response") // Error occurred while calling GitHub API +class MainActivity : ComponentActivity() { + private lateinit var client: Client + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + client = Client(applicationContext) + .setEndpoint("https://cloud.appwrite.io/v1") // Replace with your API endpoint + .setProject("[PROJECT_ID]") // Replace with your project ID + val account = Account(client) + + val myScope = CoroutineScope(Dispatchers.Main) + + myScope.launch { + val response = account.getSession("current") + // Get the provider access token + val providerAccessToken = response.providerAccessToken + + val httpClient = OkHttpClient() + val request = Request.Builder() + .url("https://api.github.com/user") // GitHub API endpoint + .header("Authorization", "Bearer $providerAccessToken") + .build() + + httpClient.newCall(request).execute().use { + if (!it.isSuccessful) { + throw IOException("Unexpected code $it") //Error occurred while calling GitHub API + } + println(it.body?.string()) // GitHub API response data + } + } } - println(response.body?.string()) // GitHub API response data } ``` ### Android (Java) - If you are using `OkHttp` library for making HTTP requests, you need to add the necessary dependencies in your project's `build.gradle` file. Here's an example of how to add the `OkHttp` dependency: ```groovy @@ -476,10 +493,15 @@ dependencies { } ``` To get this access token in your Android application, use the [`account.getSession()`](https://appwrite.io/docs/client/account?sdk=android-java#accountGetSession) endpoint. + ```java +import androidx.appcompat.app.AppCompatActivity; +import android.os.Bundle; +import android.util.Log; import io.appwrite.Client; import io.appwrite.services.Account; -import io.appwrite.coroutines.CoroutineCallback; +import io.appwrite.exceptions.AppwriteException; +import io.appwrite.models.Session; import okhttp3.Call; import okhttp3.Callback; import okhttp3.OkHttpClient; @@ -487,43 +509,53 @@ import okhttp3.Request; import okhttp3.Response; import java.io.IOException; -Client client = new Client(getApplicationContext()) - .setEndpoint("https://cloud.appwrite.io/v1") // Replace with your API endpoint - .setProject("[PROJECT_ID]"); // Replace with your project ID +public class MainActivity extends AppCompatActivity { -Account account = new Account(client); + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); -account.getSession( - "current", - new CoroutineCallback<>((result, error) -> { - if (error != null) { - error.printStackTrace(); - return; - } - - String providerAccessToken = result.getProviderAccessToken(); + Client client = new Client(getApplicationContext()) + .setEndpoint("https://cloud.appwrite.io/v1") // Replace with your API endpoint + .setProject("[PROJECT_ID]"); // Replace with your project ID - // Example Request to GitHub API - OkHttpClient httpClient = new OkHttpClient(); - Request request = new Request.Builder() - .url("https://api.github.com/user") - .header("Authorization", "Bearer " + providerAccessToken) - .build(); + Account account = new Account(client); - httpClient.newCall(request).enqueue(new Callback() { + account.getSession(new CoroutineCallback<>(new CoroutineCallback.Callback() { @Override - public void onResponse(Call call, Response response) throws IOException { - String responseBody = response.body().string(); - Log.d("Appwrite", responseBody); // GitHub API response data + public void onFailure(AppwriteException e) { + e.printStackTrace(); } @Override - public void onFailure(Call call, IOException e) { - e.printStackTrace(); + public void onCompleted(Session result) { + // Get the provider access token + String providerAccessToken = result.getProviderAccessToken(); + + // Example Request to GitHub API + OkHttpClient httpClient = new OkHttpClient(); + Request request = new Request.Builder() + .url("https://api.github.com/user") + .header("Authorization", "Bearer " + providerAccessToken) + .build(); + + httpClient.newCall(request).enqueue(new Callback() { + @Override + public void onResponse(Call call, Response response) throws IOException { + String responseBody = response.body().string(); + Log.d("Appwrite", responseBody); // GitHub API response data + } + + @Override + public void onFailure(Call call, IOException e) { + e.printStackTrace(); + } + }); } - }); - }) -); + })); + } +} ``` From 22e7980a7b2c90275b543125d9a2f095c25a2442 Mon Sep 17 00:00:00 2001 From: Dakshi Date: Wed, 19 Jul 2023 19:52:19 +0530 Subject: [PATCH 5/8] fixed swift code --- app/views/docs/oauth-providers/github.md | 40 ++++++++++-------------- 1 file changed, 16 insertions(+), 24 deletions(-) diff --git a/app/views/docs/oauth-providers/github.md b/app/views/docs/oauth-providers/github.md index dac1f0a80..bb0fea81e 100644 --- a/app/views/docs/oauth-providers/github.md +++ b/app/views/docs/oauth-providers/github.md @@ -563,35 +563,27 @@ public class MainActivity extends AppCompatActivity { To get this access token in your Apple application, use the [`account.getSession()`](https://appwrite.io/docs/client/account?sdk=apple-default#accountGetSession) endpoint. ```swift import Appwrite +import Foundation -let client = Client() - .setEndpoint("https://cloud.appwrite.io/v1") // Replace with your API endpoint - .setProject("[PROJECT_ID]") // Replace with your project ID +func main() async throws { + let client = Client() + .setEndpoint("https://cloud.appwrite.io/v1") // Replace with your API endpoint + .setProject("[PROJECT_ID]") // Replace with your project ID -let account = Account(client) + let account = Account(client) -async { - do { - let session = try await account.getSession( - sessionId: "current" - ) - - let providerAccessToken = session.providerAccessToken + let session = try await account.getSession(sessionId: "current") + // Get the provider access token + let providerAccessToken = session.providerAccessToken - // Example Request to GitHub API - let url = URL(string: "https://api.github.com/user")! - var request = URLRequest(url: url) - request.setValue("Bearer \(providerAccessToken)", forHTTPHeaderField: "Authorization") + let url = URL(string: "https://api.github.com/user")! + var request = URLRequest(url: url) + request.setValue("Bearer \(providerAccessToken)", forHTTPHeaderField: "Authorization") - let (data, _) = try await URLSession.shared.data(for: request) - - if let data = data { - if let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] { - print(json) // GitHub API response data - } - } - } catch { - print(error) // Error occurred while calling GitHub API + let (data, _) = try await URLSession.shared.data(for: request) + + if let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] { + print(json) // GitHub API response data } } ``` From 06b9054d1a8b1d3a988f2f8e44daaea43cb87319 Mon Sep 17 00:00:00 2001 From: Dakshi Goel <91268240+Dksie09@users.noreply.github.com> Date: Thu, 20 Jul 2023 06:38:41 +0530 Subject: [PATCH 6/8] minor changes Co-authored-by: Vincent (Wen Yu) Ge --- app/views/docs/oauth-providers/github.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/docs/oauth-providers/github.md b/app/views/docs/oauth-providers/github.md index bb0fea81e..d08caee63 100644 --- a/app/views/docs/oauth-providers/github.md +++ b/app/views/docs/oauth-providers/github.md @@ -392,7 +392,7 @@ promise.then( ); ``` ### Flutter (Dart) -To get this access token in your flutter application, use the [`account.getSession()`](https://appwrite.io/docs/client/account?sdk=flutter-default#accountGetSession) endpoint. +To get this access token in your Flutter application, use the [`account.getSession()`](https://appwrite.io/docs/client/account?sdk=flutter-default#accountGetSession) endpoint. ```dart import 'package:appwrite/appwrite.dart'; import 'package:http/http.dart' as http; From 166105760ea1a54604253e88466581aa47291735 Mon Sep 17 00:00:00 2001 From: Dakshi Date: Thu, 20 Jul 2023 06:44:55 +0530 Subject: [PATCH 7/8] minor changes --- app/views/docs/oauth-providers/github.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app/views/docs/oauth-providers/github.md b/app/views/docs/oauth-providers/github.md index bb0fea81e..8f9c07d0a 100644 --- a/app/views/docs/oauth-providers/github.md +++ b/app/views/docs/oauth-providers/github.md @@ -102,7 +102,9 @@ void main() async { // OAuth Login, for simplest implementation you can leave both success and // failure link empty so that Appwrite handles everything. - await account.createOAuth2Session(provider: 'github'); + await account.createOAuth2Session( + provider: "github" + ); } ``` @@ -576,6 +578,7 @@ func main() async throws { // Get the provider access token let providerAccessToken = session.providerAccessToken + // Example Request to GitHub API let url = URL(string: "https://api.github.com/user")! var request = URLRequest(url: url) request.setValue("Bearer \(providerAccessToken)", forHTTPHeaderField: "Authorization") From de50cb3cbaf2f03e6ab66a24e8de073916fa937a Mon Sep 17 00:00:00 2001 From: Dakshi Date: Thu, 20 Jul 2023 06:46:30 +0530 Subject: [PATCH 8/8] minor changes --- app/views/docs/oauth-providers/github.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/docs/oauth-providers/github.md b/app/views/docs/oauth-providers/github.md index 8f9c07d0a..e977579a1 100644 --- a/app/views/docs/oauth-providers/github.md +++ b/app/views/docs/oauth-providers/github.md @@ -394,7 +394,7 @@ promise.then( ); ``` ### Flutter (Dart) -To get this access token in your flutter application, use the [`account.getSession()`](https://appwrite.io/docs/client/account?sdk=flutter-default#accountGetSession) endpoint. +To get this access token in your Flutter application, use the [`account.getSession()`](https://appwrite.io/docs/client/account?sdk=flutter-default#accountGetSession) endpoint. ```dart import 'package:appwrite/appwrite.dart'; import 'package:http/http.dart' as http;