diff --git a/app/views/docs/oauth-providers/github.md b/app/views/docs/oauth-providers/github.md index cd4764173..e977579a1 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('github'); + await account.createOAuth2Session( + provider: "github" + ); } ``` @@ -226,7 +228,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 @@ -346,4 +348,245 @@ let account = Account(client) let session = try await account.updateSession( sessionId: "current" ) -``` \ No newline at end of file +``` +## Provider Access Token + +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) { + // Get the provider access token + 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) { + // Get the provider access token + 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 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 + +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 + } + } + } +} +``` + +### 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 androidx.appcompat.app.AppCompatActivity; +import android.os.Bundle; +import android.util.Log; +import io.appwrite.Client; +import io.appwrite.services.Account; +import io.appwrite.exceptions.AppwriteException; +import io.appwrite.models.Session; +import okhttp3.Call; +import okhttp3.Callback; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; +import java.io.IOException; + +public class MainActivity extends AppCompatActivity { + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + 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(new CoroutineCallback<>(new CoroutineCallback.Callback() { + @Override + public void onFailure(AppwriteException e) { + e.printStackTrace(); + } + + @Override + 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(); + } + }); + } + })); + } +} +``` + + +### 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 +import Foundation + +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 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 (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 + } +} +```